LINQ在内部如何工作? [英] How LINQ works internally?

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

问题描述

我喜欢在.net上使用LINQ,但我想知道它在内部如何工作?

I love to use LINQ on .net, but I wonder to know how does that works internally?

有人知道吗?

谢谢.

推荐答案

询问LINQ的特定方面更有意义.有点像询问"Windows如何工作".

It makes more sense to ask about a particular aspect of LINQ. It's a bit like asking "How Windows works" otherwise.

从C#角度来看,LINQ的关键部分对我来说是

The key parts of LINQ are for me, from a C# perspective:

  • 表达树.这些是代码作为数据的表示.例如,表达式树可以表示采用字符串参数,在其上调用Length属性,然后返回结果"的概念.这些作为数据而不是作为编译代码存在的事实意味着LINQ to SQL之类的LINQ提供程序可以对其进行分析并将其转换为SQL.
  • Lambda表达式.这些是这样的表达式:

  • Expression trees. These are representations of code as data. For instance, an expression tree could represent the notion of "take a string parameter, call the Length property on it, and return the result". The fact that these exist as data rather than as compiled code means that LINQ providers such as LINQ to SQL can analyze them and convert them into SQL.
  • Lambda expressions. These are expressions like this:

x => x * 2
(int x, int y) => x * y
() => { Console.WriteLine("Block"); Console.WriteLine("Lambda"); }

Lambda表达式将转换为代理人表达式树.

Lambda expressions are converted either into delegates or expression trees.

匿名类型.这些是这样的表达式:

Anonymous types. These are expressions like this:

new { X=10, Y=20 }

这些仍然是静态类型的,只是编译器为您生成具有属性XY的不可变类型.这些通常与var一起使用,它允许从其初始化表达式中推断出局部变量的类型.

These are still statically typed, it's just the compiler generates an immutable type for you with properties X and Y. These are usually used with var which allows the type of a local variable to be inferred from its initialization expression.

查询表达式.这些是这样的表达式:

Query expressions. These are expressions like this:

from person in people
where person.Age < 18
select person.Name

这些代码由C#编译器翻译为普通" C#3.0(即不使用查询表达式的表单).此后将应用重载解析等,这对于能够在多种数据类型中使用相同的查询语法,而编译器却不了解诸如Queryable之类的类型,这绝对是关键.上面的表达式将被翻译为:

These are translated by the C# compiler into "normal" C# 3.0 (i.e. a form which doesn't use query expressions). Overload resolution etc is applied afterwards, which is absolutely key to being able to use the same query syntax with multiple data types, without the compiler having any knowledge of types such as Queryable. The above expression would be translated into:

people.Where(person => person.Age < 18)
      .Select(person => person.Name)

  • 扩展方法.这些是静态方法,可以像第一个参数类型的实例方法一样使用.例如,这样的扩展方法:

  • Extension methods. These are static methods which can be used as if they were instance methods of the type of the first parameter. For example, an extension method like this:

    public static int CountAsciiDigits(this string text)
    {
        return text.Count(letter => letter >= '0' && letter <= '9');
    }
    

    然后可以像这样使用

    :

    can then be used like this:

    string foo = "123abc456";
    int count = foo.CountAsciiDigits();
    

    请注意,CountAsciiDigits的实现使用另一种扩展方法Enumerable.Count().

    Note that the implementation of CountAsciiDigits uses another extension method, Enumerable.Count().

    这是与语言相关的大部分内容.然后在LINQ提供程序中有标准查询运算符的实现,例如LINQ to Objects和LINQ to SQL等. //深度网站上C#的//csharpindepth.com/Talks.aspx"rel =" noreferrer>"对话"页.

    That's most of the relevant language aspects. Then there are the implementations of the standard query operators, in LINQ providers such as LINQ to Objects and LINQ to SQL etc. I have a presentation about how it's reasonably simple to implement LINQ to Objects - it's on the "Talks" page of the C# in Depth web site.

    诸如LINQ到SQL的提供程序的工作方式通常是通过Queryable类.他们将表达式树转换为其他查询格式,然后以执行这些进程外查询的结果为基础构造适当的对象.

    The way providers such as LINQ to SQL work is generally via the Queryable class. At their core, they translate expression trees into other query formats, and then construct appropriate objects with the results of executing those out-of-process queries.

    可以涵盖您感兴趣的所有内容吗?如果您仍然想知道什么特别的地方,只需编辑您的问题,我就可以解决.

    Does that cover everything you were interested in? If there's anything in particular you still want to know about, just edit your question and I'll have a go.

    这篇关于LINQ在内部如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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