包含 "AsEnumerable()" 等价的 Linq 查询
Linq Queries containing "AsEnumerable()" equivalence
这 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
class 中的扩展。此版本的扩展接收 Expression
作为参数,这些表达式树允许将您的代码转换为 sql 查询。
AsEnumerable()
returns 对象作为 IEnumerable
,因此您将使用直接在内存中执行的 Enumerable
class 中的扩展。
是的,他们做同样的事情,但方式不同。第一个查询在 SQL 数据库本身中执行所有选择、排序和条件。
然而,第二个代码段从数据库中获取所有行并将其存储在内存中。然后在对获取的数据进行排序、排序和应用条件之后,即现在在内存中。
AsEnumerable() 将查询分为两部分:
- 内部部分(AsEnumerable 之前的查询)作为 LINQ-to-SQL
执行
- 外部部分(AsEnumerable 之后的查询)作为 LINQ-to-Objects 执行
这 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
class 中的扩展。此版本的扩展接收 Expression
作为参数,这些表达式树允许将您的代码转换为 sql 查询。
AsEnumerable()
returns 对象作为 IEnumerable
,因此您将使用直接在内存中执行的 Enumerable
class 中的扩展。
是的,他们做同样的事情,但方式不同。第一个查询在 SQL 数据库本身中执行所有选择、排序和条件。
然而,第二个代码段从数据库中获取所有行并将其存储在内存中。然后在对获取的数据进行排序、排序和应用条件之后,即现在在内存中。
AsEnumerable() 将查询分为两部分:
- 内部部分(AsEnumerable 之前的查询)作为 LINQ-to-SQL 执行
- 外部部分(AsEnumerable 之后的查询)作为 LINQ-to-Objects 执行