是否有可能编译查询LINQ到对象 [英] Is it possible to compile a query for linq-to-objects

查看:88
本文介绍了是否有可能编译查询LINQ到对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个LINQ到递归循环对象查询和害怕,当物体接近更多的则1000,在网站上的100多个用户 - 我的网站将打破。所以是有可能编译的LINQ to对象的查询。

I have a linq to objects query in a recursive loop and afraid when the objects approach more then 1000 and a have more then 100 users on the site -- my website will break. so is it possible to compile a linq to objects query.

LINQ查询确实没有更多然后找到一个节点的直接孩子。

The linq query does nothing more then find the direct children of a node.

推荐答案

要明白为什么的的概念编纂的并没有真正意义的LINQ to对象查询,是非常有用的了解LINQ是如何实现的。首先,应该明确的是,写流利的语法LINQ查询在转换为等效的方法调用语法的编译时间的由C#编译器,无论你使用LINQ的变种:

To see why the concept of compilation doesn't really make sense for LINQ to Object queries, it's useful to understand how LINQ is implemented. First, it should be clear that LINQ queries written in fluent syntax are converted to the equivalent method call syntax at compile time by the C# compiler regardless of the variant of LINQ you're using:

from person in people
where person.Age < 18
select person.Name
// will be converted to:
people.Where(person => person.Age < 18).Select(person => person.Name)

从现在开始,LINQ查询基本上是一组方法调用服用一些参数,通常转化的的IEnumerable< T> 对象到另一个的IEnumerable< T> 对象。延迟执行,这是由编译不同的,仅仅是通过不采取任何对象实现了最初的IEnumerable< T> 直到你遍历输出的IEnumerable< T> 。基本上,延迟执行的方法是在它们的参数象征性地操作而不触及原有的收集,建立一个发电机,只要你喜欢,查询的东西。

From now on, the LINQ query is basically a set of method calls taking some arguments and usually transforming an IEnumerable<T> object to another IEnumerable<T> object. Deferred execution, which is a different from compilation, is simply achieved by not taking any object from the original IEnumerable<T> until you're traversing the output IEnumerable<T>. Basically, methods with deferred execution are operating on their arguments symbolically without touching the original collection, building up a generator that queries stuff as you like.

考虑到这一点,取看看lambda表达式人=> person.Age< 18 在上面的表达式。它需要一个对象,并返回一个布尔。 Lambda表达式是无类型;它们可以被视为表达树木或根据它们的类型是从推导出的上下文匿名方法。在这种情况下,该类型是从参数类型的其中,扩展方法的推断。这是LINQ到SQL和LINQ的区别对象出现。在LINQ to对象,在其中,方法只需要 Func键<人,布尔> ,而不是表达式来; Func键<人,布尔>> 。这实际上意味着,在LINQ到对象,C#编译器编译lambda表达式到一个匿名方法和产生IL的编译时间的,并传递一个委托给该方法以在哪里

With that in mind, take a look at the lambda expression person => person.Age < 18 in the above expression. It takes a Person object and returns a bool. Lambda expressions are untyped; they can be treated as expression trees or anonymous methods depending on the context their type is inferred from. In this case, the type is inferred from the parameter type of the Where extension method. This is where the distinction of LINQ to SQL and LINQ to Object comes up. In LINQ to Objects, the Where method simply takes Func<Person, bool> as opposed to Expression<Func<Person, bool>>. This essentially means that in LINQ to Objects, the C# compiler compiles the lambda expression down to an anonymous method and generates the IL at compile time and passes a delegate to that method to Where.

在其他LINQ的口味,喜欢的LINQ to SQL,拉姆达是的的编译为IL。相反,编译器生成了一个表达式树对象了lambda表达式,并传递表达式目录树中的LINQ的方法。 LINQ方法使用这些表达式目录树建立一个模型来查询的东西。当正在运行的查询,建立了使用表达式树表示查询对象模型将转换为另一件事(取决于所使用的LINQ变量)就像在LINQ SQL语句以SQL来获取数据库上执行。这个转化过程是在运行时完成,这就是我们所说的的LINQ查询编译的。

In other LINQ flavors, like LINQ to SQL, the lambda is not compiled to IL. Instead, the compiler builds up an expression tree object out of the lambda expression and passes the expression tree to LINQ methods. LINQ methods use these expression trees to build up a model for querying stuff. When the query is being run, the object model built to represent the query using the expression trees will be transformed to another thing (depending on the LINQ variant used) like SQL statements in LINQ to SQL in order to get executed on the database. This transformation process is done at runtime and it's what we call compilation of LINQ queries.

要总结起来,问题是编译的什么的?究其原因的LINQ to对象不需要在运行时编译的是,它不是摆在首位表达式树格式;它已经IL。

To sum up, the question is compile to what? The reason LINQ to Object doesn't need compilation at runtime is that it's not in expression tree format in the first place; it's already IL.

您几乎从来不需要担心LINQ中比较正常的循环性能,以对象。

You almost never need to worry about the performance of LINQ to Objects in comparison to normal looping.

这篇关于是否有可能编译查询LINQ到对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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