Do at compile time Linq-To-Objects statements also

2019-09-08 23:19发布

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条回答
迷人小祖宗
2楼-- · 2019-09-08 23:56

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);
查看更多
登录 后发表回答