Linq是如何工作的(在幕后)? [英] How does Linq work (behind the scenes)?

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

问题描述

我当时正在考虑为Lua制作Linq之类的东西,我对Linq的工作原理有一个大致的了解,但想知道是否有一篇不错的文章,或者有人可以解释C#如何使Linq成为可能

I was thinking about making something like Linq for Lua, and I have a general idea how Linq works, but was wondering if there was a good article or if someone could explain how C# makes Linq possible

注意:我的意思是在幕后,例如它如何生成代码绑定以及所有这些内容,而不是最终用户语法.

Note: I mean behind the scenes, like how it generates code bindings and all that, not end user syntax.

推荐答案

很难回答这个问题,因为LINQ有很多不同的东西.例如,坚持使用C#,涉及以下内容:

It's hard to answer the question because LINQ is so many different things. For instance, sticking to C#, the following things are involved:

  • 将查询表达式预处理"为没有查询表达式的C#",然后将其正常编译.规范中的查询表达部分确实很短-它基本上是一种机械翻译,除了"order by被翻译成OrderBy/ThenBy/etc"之外,它不假设查询的真正含义.
  • 委托用于表示具有特定签名的任意动作,作为可执行代码.
  • 表达式树用于表示同一事物,但表示为数据(可以将其检查并转换为其他形式,例如SQL)
  • Lambda表达式用于将源代码转换为委托或表达式树.
  • 大多数LINQ提供程序都使用
  • 扩展方法将静态方法调用链接在一起.这样一来,一个简单的界面(例如IEnumerable<T>)就可以有效地获取更多电量.
  • 匿名类型用于投影-您需要收集一些不同的数据,并且希望该数据的各个方面都可以使用,匿名类型使您可以将它们收集在一起.
  • 隐式类型的局部变量(var)主要用于处理匿名类型时,以维护静态类型的语言,在该语言中您可能无法明确地说出"类型的名称.
  • 迭代器块通常用于实现进程内查询,例如LINQ to Objects.
  • 使用类型推断可以使整个过程更加顺畅-LINQ中有很多通用方法,如果没有类型推断,那将非常痛苦.
  • 代码生成用于将模型(例如DBML)转换为代码
  • 部分类型用于提供对生成代码的可扩展性
  • 属性用于向LINQ提供程序提供元数据
  • Query expressions are "pre-processed" into "C# without query expressions" which is then compiled normally. The query expression part of the spec is really short - it's basically a mechanical translation which doesn't assume anything about the real meaning of the query, beyond "order by is translated into OrderBy/ThenBy/etc".
  • Delegates are used to represent arbitrary actions with a particular signature, as executable code.
  • Expression trees are used to represent the same thing, but as data (which can be examined and translated into a different form, e.g. SQL)
  • Lambda expressions are used to convert source code into either delegates or expression trees.
  • Extension methods are used by most LINQ providers to chain together static method calls. This allows a simple interface (e.g. IEnumerable<T>) to effectively gain a lot more power.
  • Anonymous types are used for projections - where you have some disparate collection of data, and you want bits of each of the aspects of that data, an anonymous type allows you to gather them together.
  • Implicitly typed local variables (var) are used primarily when working with anonymous types, to maintain a statically typed language where you may not be able to "speak" the name of the type explicitly.
  • Iterator blocks are usually used to implement in-process querying, e.g. for LINQ to Objects.
  • Type inference is used to make the whole thing a lot smoother - there are a lot of generic methods in LINQ, and without type inference it would be really painful.
  • Code generation is used to turn a model (e.g. DBML) into code
  • Partial types are used to provide extensibility to generated code
  • Attributes are used to provide metadata to LINQ providers

很明显,LINQ不仅使用了很多这些,但是取决于它们的是不同的LINQ技术.

Obviously a lot of these aren't only used by LINQ, but different LINQ technologies will depend on them.

如果您可以提供有关您感兴趣的方面的更多信息,我们也许可以提供更多详细信息.

If you can give more indication of what aspects you're interested in, we may be able to provide more detail.

如果您对有效地实现LINQ to Objects感兴趣,那么您可能会对几周前我在DDD上阅读Reading的演讲感兴趣-基本是在一小时内尽可能多地实现LINQ to Objects.到最后,我们还远远没有完成,但是它应该可以很好地说明您需要做的事情(以及缓冲/流,迭代器块,查询表达式转换等).视频还没有上传(我还没有提供下载代码),但是如果您有兴趣,请给我发送电子邮件到skeet@pobox.com,当它们启动时我会通知您. . (我也可能会在博客上写这篇文章.)

If you're interested in effectively implementing LINQ to Objects, you might be interested in a talk I gave at DDD in Reading a couple of weeks ago - basically implementing as much of LINQ to Objects as possible in an hour. We were far from complete by the end of it, but it should give a pretty good idea of the kind of thing you need to do (and buffering/streaming, iterator blocks, query expression translation etc). The videos aren't up yet (and I haven't put the code up for download yet) but if you're interested, drop me a mail at skeet@pobox.com and I'll let you know when they're up. (I'll probably blog about it too.)

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

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