During compilation, are Linq-To-Objects expressions also converted to expression tree objects?

At compile time, LINQ statements working in IQueryable<T> operations (thus Linq-to-SQL and Linq-to-Entities) are converted to expression tree objects that represent code as data.

a) LINQ operations that work on IEnumerable<T> (thus LINQ-to-Objects) are also converted to expression trees?

b) If not, what happens to LINQ-to-Object statements at compile time? Does the compiler just translate them into the corresponding method calls? For example, this is the following Linq-to-Objects statement:

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

translates by the compiler into something similar to the following:

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

Thank you

0
linq-to-objects
source share
1 answer

If you mirror the code, IEnumerable extension methods actually contain an implementation for what you are trying to do. If you want to build an expression tree assembly, just link the AsQueryable () extension method at the beginning so that your query becomes:

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

All Articles