在LINQ理解.AsEnumerable()到SQL [英] Understanding .AsEnumerable() in LINQ to SQL

查看:182
本文介绍了在LINQ理解.AsEnumerable()到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下的LINQ to SQL查询:

Given the following LINQ to SQL query:

var test = from i in Imports
           where i.IsActive
           select i;

该间preTED SQL语句是:

The interpreted SQL statement is:

SELECT [t0].[id] AS [Id] .... FROM [Imports] AS [t0] WHERE [t0].[isActive] = 1

说我想执行的选择不能转换为SQL一些动作。它我的理解,传统的方式来完成,这是做 AsEnumerable()从而将其转换为一个可行的对象。

Say I wanted to perform some action in the select that cannot be converted to SQL. Its my understanding that the conventional way to accomplish this is to do AsEnumerable() thus converting it to a workable object.

鉴于这种更新code:

Given this updated code:

var test = from i in Imports.AsEnumerable()
           where i.IsActive
           select new 
           { 
               // Make some method call 
           };

和更新SQL:

SELECT [t0].[id] AS [Id] ... FROM [Imports] AS [t0] 

注意,缺乏执行的SQL语句的where子句。

Notice the lack of a where clause in the executed SQL statement.

这是否意味着整个进口表被缓存到内存?
将在所有如果表中含有大量的记录,这种缓慢的表现?

Does this mean the entire "Imports" table is cached into memory? Would this slow performance at all if the table contained a large amount of records?

帮助我了解什么是真正幕后发生的事情在这里。

Help me to understand what is actually happening behind the scenes here.

推荐答案

原因 AsEnumerable

AsEnumerable(TSource)(IEnumerable的(TSource))
  可用于查询之间进行选择
  实现时的序列
  实现IEnumerable(T),但也有
  一组不同的公共查询
  可用的方法

AsEnumerable(TSource)(IEnumerable(TSource)) can be used to choose between query implementations when a sequence implements IEnumerable(T) but also has a different set of public query methods available

所以,当你调用了Where方法之前,你调用一个不同的方法在哪里从IEnumerable.Where。这Where语句是LINQ转换为SQL,新在哪里了IEnumerable一个接受一个I​​Enumerable,列举并产生相匹配的项目。这也解释了为什么你看到正在生成不同的SQL。该表将全额取自数据库之前,如果扩展会在你的code的第二个版本适用。这可能产生严重的瓶颈,因为整个表必须在存储器中,或更糟糕的整个表将具有服务器之间行进。允许SQL Server执行在哪里,做它最好的。

So when you were calling the Where method before, you were calling a different Where method from the IEnumerable.Where. That Where statement was for LINQ to convert to SQL, the new Where is the IEnumerable one that takes an IEnumerable, enumerates it and yields the matching items. Which explains why you see the different SQL being generated. The table will be taken in full from the database before the Where extension will be applied in your second version of the code. This could create a serious bottle neck, because the entire table has to be in memory, or worse the entire table would have to travel between servers. Allow SQL server to execute the Where and do what it does best.

这篇关于在LINQ理解.AsEnumerable()到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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