IEnumerable的< T>和IQueryable的< T>澄清? [英] IEnumerable<T> and IQueryable<T> clarification?

查看:186
本文介绍了IEnumerable的< T>和IQueryable的< T>澄清?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读这个的问题后, 我需要澄清一些事情

after reading this question , i need to clear up some things

IQueryable<Customer> custs = from c in db.Customers
where c.City == "<City>"
select c;

IEnumerable<Customer> custs = from c in db.Customers
where c.City == "<City>"
select c;

问题:

1)它是确定的说法是:在第一个查询中SQLServer是运行在整个操作过程,包括where子句和返回的相关行 - 而第二个呢 SELECT * ...返回所有行到C#和 的过滤器?

1) Is it ok to say that : in the first query the sqlServer is running the whole operation including where clause and returning ONLY the relevant rows - while the second one does SELECT * ... and returns all rows into c# and THEN filters ?

2)什么样,如果我有一个的集合的仅仅是 - 在内存中。 ( VAR lstMyPerson =新的名单,其中,MyPerson&GT;()

2) what about if i have a collection merely - in memory. ( var lstMyPerson = new List<MyPerson>() )

    IQueryable<MyPerson> lst = from c in lstMyPerson 
    where c.City == "<City>"
    select c;

VS

    IEnumerable<MyPerson> custs = from c in lstMyPerson 
    where c.City == "<City>"
    select c;

这将是执行中的差异呢?

what will be the difference in execution now ?

推荐答案

1:不,那是不正确

既然你只的存储的结果到的IEnumerable&LT;客户&GT; ,但仍然有完全相同的前pression的产生的结果,他们都将在服务器上执行,只返回相关行。

Since you're only storing the result into an IEnumerable<Customer>, but still have the exact same expression that produces the result, they will both execute on the server and return only the relevant rows.

您将获得在行为的差异这一点:

You would get the difference in behavior with this:

IEnumerable<Customer> custs = from c in (IEnumerable<Customer>)db.Customers
    where c. City == "<City>"
    select c;

在这种情况下会迫使 db.Customers 收集用作的IEnumerable&LT; T&GT; ,其中当枚举将获取整个集合。

In this case you are forcing the db.Customers collection to be used as IEnumerable<T>, which when enumerated will fetch the entire collection.

需要注意的是这样的:

IEnumerable<Customer> x = from c in db.Customers
                          where c.City == "<City>"
                          select c;

是不一样的,因为这

is not the same as this:

IEnumerable<Customer> x = from c in db.Customers
                          select c;
IEnumerable<Customer> y = x.Where(c => c.City == "<City>");

在第一种情况下,在其中,子句将是SQL的组成部分,在第二个它不会。这就是为什么链接的问题/答案涉及的不同,而你的code没有。

In the first case, the where clause will be a part of the SQL, in the second it won't. That is why the linked question/answer involves a difference, whereas your code does not.

另外请注意,的只有的你写不会,其实在服务器上执行任何东西,因为它们会有效地只存储一个延迟集合的语句。如果你继续前进,列举这些集合,在这一点上的相关位将在服务器上执行。

Also note that only the statements you have written will not in fact execute anything at all on the server, since they will effectively only store a lazy collection. If you go ahead and enumerate those collections, at that point the relevant bits will be executed on the server.

2:名单,其中,T&GT; 不执行或对扩展方法的IQueryable&LT; T&GT; ,也不会对LINQ运营商参与返回任何兼容的IQueryable&LT; T&GT;

2: List<T> does not implement or have extension methods for IQueryable<T>, nor will the LINQ operators involved return anything compatible with IQueryable<T>

在这种情况下,第一个将无法编译。

In this case, the first will not compile.

这篇关于IEnumerable的&LT; T&GT;和IQueryable的&LT; T&GT;澄清?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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