Where子句不适用于LINQ IQueryable查询 [英] Where clause not working on LINQ IQueryable query

查看:250
本文介绍了Where子句不适用于LINQ IQueryable查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Linq to Entities,当我对用书面LINQ语句创建的Linq对象执行Where兰巴表达式时,where子句不起作用.

Using Linq to Entities, the where clause is not working when I do a Where lamba expression on a Linq object created with a written LINQ statement.

这不起作用.查询执行,但结果未经过滤返回.

This does not work. Query executes, but results are returned unfiltered.

var myQuery = (from l in db.MyTable
                     select l);
myQuery.Where(r => availableStatusList.Contains(r.Status));
var myObj = myQuery.ToList();  

这确实有效.执行查询,并正确过滤返回的结果.

This does work. Query executes, AND results are returned filtered properly.

var myQuery = (from l in db.MyTable
                     select l).Where(r => availableStatusList.Contains(r.Status));
var myObj = myQuery.ToList();

据我了解,这两个都应返回相等的结果.第一个为什么不听Where子句的原因是什么?

As far as I understand, both of these should return equivalent results. Any reason why the first one does not listen to the Where clause?

推荐答案

Where子句在调用时不会创建新对象;通过将集合包装在最终将要运行的过滤器中,它将过滤器应用于现有集合.但是,由于它是一个纯函数,因此该过滤器的返回值需要返回到原始引用myQuery,这就是第二个示例起作用的原因...您已将结果通过Where()子句链接回了myQuery.您返回过滤后的集合,并使用ToList()实现延迟的查询.

A Where clause does not create a new object when invoked; it applies a filter to an existing collection by wrapping the collection in a filter that will eventually be run. However, since it is a pure function, the return value of that filter needs to be returned to the original reference myQuery, which is why the second example works...you have chained the result through the Where() clause back to myQuery. You return the filtered collection back, materializing the deferred query with ToList().

在第一个示例中,假设过滤器直接应用于集合,但这不是LINQ的工作原理,因为调用函数时过滤器不会立即修改集合.相反,它仅适用于将附加"到原始Queryable集合并且将使用ToList()解析查询的情况下将被应用的谓词.

In the first example, you are assuming that the filter is applied directly to the collection, but that isn't how LINQ works, since the filter doesn't modify the collection immediately when the function is called. Rather, it only applies a predicate that WILL be applied provided it is "attached" to the original Queryable collection and you resolve the query with ToList().

有时将延期执行视为一系列承诺比较容易.

It is sometimes easier to think about deferred execution as a series of promises.

如果我给你一美元,您就有一美元.如果我收取25美分的税费,则表示立即解决了我们的交易(查询).

If I give you a dollar, you have a dollar. If I take 25 cents for taxes, I've immediately resolved our transaction (query).

但是,如果我保证在星期二给你一美元,我已经退回了Queryable<T> 承诺.

However, if I promise to give you a dollar on Tuesday, I've returned a Queryable<T> promise.

但是,从现在到星期二,可能会发生多个事件.我可以将一个过滤器链接到税(25美分),一个口香糖过滤器(25美分)或任何其他过滤器.

Multiple events could occur however between now and Tuesday. I could chain a filter for taxes (25 cents), a filter for a stick of gum (25 cents), or any other filter.

但是,我们的簿记系统有一个警告.我们不能只对承诺的总金额调用.Taxes()(我们的Where子句)并期望其更新.我们必须根据承诺金额记录我们的交易,方法是将过滤器返回到原始变量,并使用针对我们承诺金额的交易进行更新.

However, our bookkeeping system has a caveat. We can't just call .Taxes() (our Where clause) on the total promised amount and expect it to update. We have to record our transaction against the promised amount by returning the filter to the original variable, updating it with the transaction that has occurred against our promised amount.

myQuery = myQuery.Where(condition);

在星期二,当您要收取款项时(通过在一系列的链接的承诺/过滤器上调用ToList()),我从承诺的金额中扣除发生了的事情,从而产生了50美分的支出.那是在工作中推迟执行.

On Tuesday, when you come to collect your payment (by calling ToList() on the series of chained promises/filters), I deduct what has occurred from what was promised, resulting in a 50 cent payout. That's deferred execution at work.

这篇关于Where子句不适用于LINQ IQueryable查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆