如何确定LINQ查询将是LINQ to SQL还是LINQ to Objects? [英] How can I determine if a LINQ query is going to be LINQ to SQL vs. LINQ to Objects?

查看:92
本文介绍了如何确定LINQ查询将是LINQ to SQL还是LINQ to Objects?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常情况下,LINQ to SQL和LINQ to Object之间的区别不是问题(我认为一开始就把它当作问题是过早的优化),但是我如何确定正在发生的事情呢?

Usually the distinction between LINQ to SQL and LINQ to Objects isn't an issue (and I think making it an issue from the get-go is premature optimization), but how can I determine which is happening?

知道在编写代码时很有用,但是我担心有时只能在运行时才能确定.

It would be useful to know when writing the code, but I fear one can only be sure at run time sometimes.

推荐答案

在Linq-To-Sql和Linq-To-Objects之间进行区分不是 not 微优化.后者要求将所有数据加载到内存中,然后再开始对其进行过滤.当然,这可能是一个主要问题.

It's not micro optimization to make the distinction between Linq-To-Sql and Linq-To-Objects. The latter requires all data to be loaded into memory before you start filtering it. Of course, that can be a major issue.

大多数LINQ方法都使用延迟执行,这意味着它只是在构建 query ,但尚未执行(例如SelectWhere).很少有人会执行查询并将结果具体化为内存中的集合(例如ToLIstToArray).如果您使用AsEnumerable,那么您还使用了Linq-To-Objects,并且在其后的任何部分均未生成SQL,这意味着必须将数据加载到内存中(但仍使用延迟执行).

Most LINQ methods are using deferred execution, which means that it's just building the query but it's not yet executed (like Select or Where). Few others are executing the query and materialize the result into an in-memory collection (like ToLIst or ToArray). If you use AsEnumerable you are also using Linq-To-Objects and no SQL is generated for the parts after it, which means that the data must be loaded into memory (yet still using deferred execution).

因此请考虑以下两个查询.第一个选择并过滤数据库:

So consider the following two queries. The first selects and filters in the database:

var queryLondonCustomers = from cust in db.customers
                           where cust.City == "London"
                           select cust;

第二个选择全部并通过Linq-To-Objects进行过滤:

whereas the second selects all and filters via Linq-To-Objects:

var queryLondonCustomers = from cust in db.customers.AsEnumerable()
                           where cust.City == "London"
                           select cust;

后者具有一个优点:您可以使用任何.NET方法,因为它不需要转换为SQL(例如!String.IsNullOrWhiteSpace(cust.City)).

The latter has one advantage: you can use any .NET method since it doesn't need to be translated to SQL (e.g. !String.IsNullOrWhiteSpace(cust.City)).

如果只得到IEnumerable<T>,则不能确定它实际上是查询还是已经在内存中的对象.由于

If you just get something that is an IEnumerable<T>, you can't be sure if it's actually a query or already an in-memory object. Even the try-cast to IQueryable<T> will not tell you for sure what it actually is because of the AsQueryable-method. Maybe you could try-cast it to a collection type. If the cast succeeds you can be sure that it's already materialized but otherwise it doesn't tell you if it's using Linq-To-Sql or Linq-To-Objects:

bool isMaterialized = queryLondonCustomers as ICollection<Customer> != null;

相关: EF ICollection Vs列表Vs IEnumerable Vs IQueryable

这篇关于如何确定LINQ查询将是LINQ to SQL还是LINQ to Objects?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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