动态生成 Linq/Lambda Where 子句

发布于 2024-11-06 05:14:28 字数 806 浏览 0 评论 0 原文

我一直在这里和谷歌搜索,但我不知所措。我需要让用户使用表单在数据库中搜索报告。如果表单上的某个字段具有值,则应用程序将获取该字段设置为该值的任何报告。如果表单上的字段留空,应用程序将忽略它。我该怎么做?理想情况下,我只想将Where子句编写为字符串并将那些不为空的子句添加在一起。

.Where("Id=1")

我听说这应该可以工作,但我不断收到错误:“无法在当前上下文范围内解析确保所有引用的变量都是在范围内……”。

另一种方法是提取所有报告,然后一次过滤一个 where 子句。我对此犹豫不决,因为 1. 这是网络上的大量数据,2. 用户端需要进行大量处理。我想利用服务器的处理能力。我听说在实际请求之前它不会查询。因此,做这样的事情

    var qry = ctx.Reports
          .Select(r => r);

实际上并不会运行查询,直到我这样做:

    qry.First()

但是如果我开始这样做:

    qry = qry.Where(r => r.Id = 1).Select(r => r);
    qry = qry.Where(r => r.reportDate = '2010/02/02').Select(r => r);

那会运行查询吗?因为我要为其添加一个 where 子句。我想要一个简单的解决方案...在最坏的情况下我会使用查询生成器的东西...但我宁愿避免这种情况(看起来很复杂)。

有什么建议吗? :)

I've been searching here and Google, but I'm at a loss. I need to let users search a database for reports using a form. If a field on the form has a value, the app will get any reports with that field set to that value. If a field on a form is left blank, the app will ignore it. How can I do this? Ideally, I'd like to just write Where clauses as Strings and add together those that are not empty.

.Where("Id=1")

I've heard this is supposed to work, but I keep getting an error: "could not be resolved in the current scope of context Make sure all referenced variables are in scope...".

Another approach is to pull all the reports then filter it one where clause at a time. I'm hesitant to do this because 1. that's a huge chunk of data over the network and 2. that's a lot of processing on the user side. I'd like to take advantage of the server's processing capabilities. I've heard that it won't query until it's actually requested. So doing something like this

    var qry = ctx.Reports
          .Select(r => r);

does not actually run the query until I do:

    qry.First()

But if I start doing:

    qry = qry.Where(r => r.Id = 1).Select(r => r);
    qry = qry.Where(r => r.reportDate = '2010/02/02').Select(r => r);

Would that run the query? Since I'm adding a where clause to it. I'd like a simple solution...in the worst case I'd use the Query Builder things...but I'd rather avoid that (seems complex).

Any advice? :)

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

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

发布评论

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

评论(3

紫瑟鸿黎 2024-11-13 05:14:28

Linq 会延迟记录获取,直到必须获取记录为止。
这意味着堆叠Where 子句只是向查询添加AND/OR 子句,但仍然不执行。

生成的查询的执行将在您尝试获取记录(First、Any 等)、记录列表(ToList())或枚举它们(foreach)的精确时刻完成。

.Take(N) 不被视为获取记录 - 而是在查询中添加 (SELECT TOP N / LIMIT N)

Linq delays record fetching until a record must be fetched.
That means stacking Where clauses is only adding AND/OR clauses to the query, but still not executing.

Execution of the generated query will be done in the precise moment you try to get a record (First, Any etc), a list of records(ToList()), or enumerate them (foreach).

.Take(N) is not considered fetching records - but adding a (SELECT TOP N / LIMIT N) to the query

淡莣 2024-11-13 05:14:28

不,这不会运行查询,您可以通过这种方式构建查询,如果它有助于可读性,那么实际上更好。在这种情况下,您正在利用惰性求值。

仅当您使用 foreach 枚举结果或强制对查询结果进行急切求值(即使用 .ToList() 或以其他方式强制求值)时,查询才会运行,即使用 First()Single() 计算单个结果。

No, this will not run the query, you can structure your query this way, and it is actually preferable if it helps readability. You are taking advantage of lazy evaluation in this case.

The query will only run if you enumerate results from it by using i.e. foreach or you force eager evaluation of the query results, i.e. using .ToList() or otherwise force evaluation, i.e evaluate to a single result using i.e First() or Single().

留一抹残留的笑 2024-11-13 05:14:28

尝试查看此动态 Linq dll 几年前发布 - 它仍然工作得很好并且看起来正是您正在寻找的东西。

Try checking out this dynamic Linq dll that was released a few years back - it still works just fine and looks to be exactly what you are looking for.

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