在编译时,Linq-To-Objects语句是否也转换为Expression Tree对象? [英] Do at compile time Linq-To-Objects statements also get translated into Expression Tree objects?

查看:64
本文介绍了在编译时,Linq-To-Objects语句是否也转换为Expression Tree对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译时,在IQueryable<T>上运行的LINQ语句(因此是Linq-to-SQL和Linq-to-Entities语句)被转换为一个表达式树对象,该对象将代码作为数据呈现.

At compile time LINQ statements that operate on IQueryable<T> ( thus Linq-to-SQL and Linq-to-Entities statements ) get translated into an expression tree objects that present code as data.

a)在IEnumerable<T>上运行的LINQ语句(也就是LINQ-to-Objects)也被翻译成表达式树吗?

a) Do LINQ statements that operate on IEnumerable<T> ( thus LINQ-to-Objects ) also get translated into expression trees?

b)如果不是,那么在编译时使用LINQ-to-Object语句会发生什么?编译器是否只是将它们转换为适当的方法调用?例如,是下一个Linq-to-Objects语句:

b) If not, what happens with LINQ-to-Object statements at compile time? Does compiler simply translate them into appropriate method calls? For example, is the next Linq-to-Objects statement:

var results = collection.Select(item => item.id).Where(id => id > 10);

编译器将其翻译为类似于以下内容的内容:

translated by compiler into something similar to the following:

var results = Enumerable.Where(
                  Enumerable.Select(collection, item => item.id),
                  id => id > 10
              );

谢谢

推荐答案

如果您反映代码,则IEnumerable扩展方法实际上包含您要执行的操作的实现.如果您希望它构建一个表达式树,只需在开始时就链接AsQueryable()扩展方法,这样您的查询就会变成:

If you reflect the code, the IEnumerable extension methods actually contain the implementation for whatever you're trying to do. If you are wanting it build build an expression tree, simply chain the AsQueryable() extension method at the beginning so your query becomes:

var results = collection.AsQueryable().Select(item => item.id).Where(id => id > 10);

这篇关于在编译时,Linq-To-Objects语句是否也转换为Expression Tree对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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