动态 LINQ - 动态、服务器端Where 子句

发布于 2024-10-10 05:37:33 字数 946 浏览 0 评论 0原文

我一直在阅读动态 LINQ 和谓词构建器。我对一些后端物流有点不清楚,也有点困惑。我试图为我的一个应用程序提供报告/高级搜索选项,其中涉及讨厌的分组最大查询。

我可以轻松地将它们组合在一起以编程方式过滤结果,但是,出于性能原因,我当然更喜欢在 SQL 服务器上执行 where 子句,而不是在 .net 代码中执行。我不确定哪些解决方案实际执行 SQL 中的谓词,而不是客户端/.NET 端。下面是我想使用 LINQ 执行的操作的示例:

from fbd in db.FooBarDatas
join max_fbd in
    (from fbd in db.FooBarDatas
     group fbd by new { fbd.FooID, fbd.BarID } into grp
     select new { MaxFooBarDataID = grp.Max(fbd => fbd.FooBarDataID }
    )
on fbd.FooBarDataID equals max_fbd.MaxFooBarDataID
select new
{
    FooBarDataID = fbd.FooBarDataID,
    NormalizedPropertyName1 = fbd.Column1,
    NormalizedPropertyName2 = fbd.Column2,
    NormalizedPropertyName3 = fbd.Column3,
    etc...
}

这是我的基本查询。最后,我将它们全部放入通用链接数据对象中,因为我想要从查询中动态提取的许多数据都来自外部表,并且我希望它们全部作为直接属性引用。

现在我想要一种方法,可以向此查询添加一大堆“where”子句条件,以便生成有意义的报告。 “where”子句将根据用户给出的参数而有所不同。记录集相当大,所以我再次寻找一种在 SQL 端进行过滤的方法。

谁能提供一个简单的例子来说明如何实现这一点?预先感谢您的任何帮助。

I have been reading up on dynamic LINQ and predicate builders. I'm a bit unclear and a bit confused about some of the backend logistics. I am trying to offer a reports/advanced search option for one of my applications, involving a nasty groupwise-max query.

I can easily put this together to filter results programmatically, but, for performance reasons, I'd certainly prefer to have the where clause executed on the SQL server, and not in the .net code. I'm unsure as to which solutions actually execute the predicates in SQL and not client/.NET side. Here's an example of what I'd like to do with LINQ:

from fbd in db.FooBarDatas
join max_fbd in
    (from fbd in db.FooBarDatas
     group fbd by new { fbd.FooID, fbd.BarID } into grp
     select new { MaxFooBarDataID = grp.Max(fbd => fbd.FooBarDataID }
    )
on fbd.FooBarDataID equals max_fbd.MaxFooBarDataID
select new
{
    FooBarDataID = fbd.FooBarDataID,
    NormalizedPropertyName1 = fbd.Column1,
    NormalizedPropertyName2 = fbd.Column2,
    NormalizedPropertyName3 = fbd.Column3,
    etc...
}

So this is my basic query. I throw them all into a generic link data object at the end because lots of the data that I'll want to pull dynamically from the query comes from foreign tables and I want them all to be referenced as direct properties.

Now I want a way that I can add a whole bunch of "where" clause conditions to this query in order to generate a meaningful report. The "where" clause will be different depending on the parameters given by the user. The record set is rather large, so again, I'm looking for a way to do the filtering on the SQL side.

Can anyone provide a simple example on how this can be accomplished? Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风尘浪孓 2024-10-17 05:37:33

我相信我在看到一个相当奇怪的文章标题后找到了答案。

http://www.weirdlover.com/2010/05/11/iqueryable-can-kill-your-dog-steal-your-wife-kill-your-will-to-live-etc/

看起来 IQueryable 在请求迭代器之前实际上并没有进行任何类型的实际 TSQL 编译。这是一个非常简单的示例:

// this does not actually call a TSQL query
IQueryable<FooBar> q = (from fb in db.FooBars select fb);
// this line still does not call a TSQL query
q = q.Where(fb => fb.FooID == 3);
// this line still does not call a TSQL query
q = q.Where(fb => fb.BarID == 74)
// this line finally compiles a TSQL statement, calls the query, and
// gathers the results:
List<FooBar> results = q.ToList();

IQueryable 对象是一个按需评估对象。尝试之后,我发现一些充满 UDF 的非常复杂的语句仍然几乎立即返回。另一篇文章解释了此过滤与服务器端过滤之间的区别,可以在这里找到:

http://www .sellsbrothers.com/posts/details/12614

我希望这可以帮助任何其他想要做我正在做的事情的人。

I believe that I found the answer after coming across a rather strange article title.

http://www.weirdlover.com/2010/05/11/iqueryable-can-kill-your-dog-steal-your-wife-kill-your-will-to-live-etc/

It seems that IQueryable does not actually do any kind of actual TSQL compilation until the iterator is requested. Here is a very simple example:

// this does not actually call a TSQL query
IQueryable<FooBar> q = (from fb in db.FooBars select fb);
// this line still does not call a TSQL query
q = q.Where(fb => fb.FooID == 3);
// this line still does not call a TSQL query
q = q.Where(fb => fb.BarID == 74)
// this line finally compiles a TSQL statement, calls the query, and
// gathers the results:
List<FooBar> results = q.ToList();

the IQueryable object is an evaluate on demand object. After trying it out, I found some pretty complex statements rife with UDF's were still returning back almost instantly. Another article that explains the difference between this and server-side filtering can be found here:

http://www.sellsbrothers.com/posts/details/12614

I hope that this helps anyone else looking to do what I was doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文