LINQ编码如何工作?幕后发生了什么? [英] How does coding with LINQ work? What happens behind the scenes?

查看:69
本文介绍了LINQ编码如何工作?幕后发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如:

m_lottTorqueTools = (From t In m_lottTorqueTools _
                     Where Not t.SlotNumber = toolTuple.SlotNumber _
                     And Not t.StationIndex = toolTuple.StationIndex).ToList

这里发生什么算法?后台是否存在嵌套的for循环?它会为这些字段构造一个哈希表吗?我很好奇.

What algorithm occurs here? Is there a nested for loop going on in the background? Does it construct a hash table for these fields? I'm curious.

推荐答案

查询表达式通常会转换为扩展方法调用. (他们没有,但是99.9%的查询使用IQueryable<T>.)

Query expressions are translated into extension method calls, usually. (They don't have to be, but 99.9% of queries use IEnumerable<T> or IQueryable<T>.)

该方法执行的确切算法因方法而异.例如,您的示例查询不会使用任何哈希表,但是会使用联接或分组操作.

The exact algorithm of what that method does varies from method to method. Your sample query wouldn't use any hash tables, but joins or grouping operations do, for example.

简单的Where调用在C#中转换为类似的东西(使用迭代器块,据我所知,目前在VB中尚不可用):

The simple Where call translates to something like this in C# (using iterator blocks, which aren't available in VB at the moment as far as I'm aware):

 public static IEnumerable<T> Where(this IEnumerable<T> source,
     Func<T, bool> predicate)
 {
     // Argument checking omitted
     foreach (T element in source)
     {
         if (predicate(element))
         {
             yield return element;
         }
     }
 }

谓词作为委托(如果使用IQueryable<T>,则为表达式树)提供,并在序列中的每个项目上调用.流式传输结果并推迟执行-换句话说,在您开始从结果中请求项目之前,什么也不会发生,即使如此,它也只能提供提供下一个结果所需的一切.某些操作符不会被延迟(基本上是那些返回单个值而不是序列的操作符),而某些缓冲输入的内容(例如Reverse必须先读取序列的末尾才能返回任何结果,因为最后一个结果它读取的是它必须产生的第一个).

The predicate is provided as a delegate (or an expression tree if you're using IQueryable<T>) and is called on each item in the sequence. The results are streamed and execution is deferred - in other words, nothing happens until you start asking for items from the result, and even then it only does as much as it needs to in order to provide the next result. Some operators aren't deferred (basically the ones which return a single value instead of a sequence) and some buffer the input (e.g. Reverse has to read to the end of the sequence before it can return any results, because the last result it reads is the first one it has to yield).

恐怕无法提供每个LINQ运算符的详细信息,这超出了单个答案的范围,但是,如果您对特定的问题有疑问,我相信我们可以提供.

It's beyond the scope of a single answer to give details of every single LINQ operator I'm afraid, but if you have questions about specific ones I'm sure we can oblige.

我应该补充一点,如果您使用的是LINQ to SQL或其他基于IQueryable<T>的提供程序,则情况会大不相同. Queryable类构建查询(在提供者的帮助下,该提供者首先实现IQueryable<T>),然后通常由提供者将查询转换为更合适的形式(例如SQL).确切的详细信息(包括缓冲,流式传输等)将完全取决于提供程序.

I should add that if you're using LINQ to SQL or another provider that's based on IQueryable<T>, things are rather different. The Queryable class builds up the query (with the help of the provider, which implements IQueryable<T> to start with) and then the query is generally translated into a more appropriate form (e.g. SQL) by the provider. The exact details (including buffering, streaming etc) will entirely depend on the provider.

这篇关于LINQ编码如何工作?幕后发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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