这 2 个查询在功能上是否等效?
1)
var z=Categories
.Where(s=>s.CategoryName.Contains("a"))
.OrderBy(s => s.CategoryName).AsEnumerable()
.Select((x,i)=>new {x.CategoryName,Rank=i});
2)
var z=Categories.AsEnumerable()
.Where(s=>s.CategoryName.Contains("a"))
.OrderBy(s => s.CategoryName)
.Select((x,i)=>new {x.CategoryName,Rank=i});
我的意思是,查询中“AsNumerable()”的顺序是否会改变从客户端检索到的数据项的数量,或者它们的检索方式?
谢谢你的帮助。
最佳答案
Are this 2 queries functionally equivalent?
如果等同于最终结果,那么可能是(取决于提供者如何实现这些操作),不同之处在于您使用内存扩展的第二个查询。
I mean, does the order of "AsNumerable()" in the query change the number of data items retrieved from the client, or the way they are retrieved?
是的,在第一个查询中,Where
和 OrderBy
将被转换为 SQL 而 Select
将在内存中执行。
在您的第二个查询中,数据库中的所有信息都被带到内存中,然后在内存中进行过滤和转换。
Categories
可能是一个 IQueryable
,因此您将使用 Queryable
类中的扩展。此版本的扩展接收一个 Expression
作为参数,这些表达式树允许将您的代码转换为 sql 查询。
AsEnumerable()
将对象作为 IEnumerable
返回,因此您将使用直接在内存中执行的 Enumerable
类中的扩展.
关于c# - 包含 "AsEnumerable()"等效项的 Linq 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36872594/