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

查看:26
本文介绍了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)有效地获得更多功能.
  • 匿名类型用于预测 - 您有一些不同的数据集合,并且您想要该数据的每个方面的位,匿名类型允许您将它们收集在一起.
  • 隐式类型化的局部变量 (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天全站免登陆