问题
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) Do LINQ statements that operate on IEnumerable<T>
( thus LINQ-to-Objects ) also get translated into expression trees?
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
);
thank you
回答1:
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);
来源:https://stackoverflow.com/questions/8405423/do-at-compile-time-linq-to-objects-statements-also-get-translated-into-expressio