The LINQ Query method provides a total of two extension methods, under the System.Linq namespace, there are two static classes: The Enumerable class, which extends for collections that inherit the Ienumerable<t> interface; Queryable class, Extended for collections that inherit the Iqueryable<t> interface. We will find that the interface iqueryable<t> actually inherits the Ienumerable<t> interface, so why should Microsoft design two sets of extension methods?
From the LINQ query functionality we know that there are actually three categories: LINQ to OBJECTS, LINQ to SQL, and LINQ to XML. In fact, the two interfaces of micro-design are mainly for LINQ to Objects and LINQ to SQL, both of which are completely different from the internal processing mechanism of queries. For LINQ to OBJECTS, the local collection is sorted and queried using the extension method in enumerable, and the query parameter accepts that func<>,func<> is called a predicate expression, which is equivalent to a delegate. For LINQ to SQL, the extension method in queryable is used, which accepts expression<>.
So, when to use Iqueryable<t>, when to use IENUMERABLE<T>
First, let's look at the Code for LINQ to SQL:
Using (var context = new northwindentities()) { var ordertmp = context. Orders.where (p=>p.customerid=="RATTC"); var orders = Ordertmp.where (p = = p.orderdate > new DateTime(1997, 1, 1)); foreach (var order in orders) { Console. WriteLine ("OrderId:" + order.) OrderID); } } |
With the IntelliSense of VS, we can see where the return type is IQueryable, and the argument is of type expression:
Let's look at this piece of code again:
Using (var context = new northwindentities()) { var ordertmp = context. Orders.where (p = = P.customerid = = "RATTC"). AsEnumerable (); var orders = Ordertmp.where (p = = p.orderdate > new DateTime(1997, 1, 1)); foreach (var order in orders) { Console. WriteLine ("OrderId:" + order.) OrderID); } } |
The difference in this code is that we return the LINQ query to the IEnumerable type, and we look at the IntelliSense effect of VS:
Since we added AsEnumerable () to the LINQ query, we can see in the second statement that the return type has changed to IEnumerable, and the argument becomes the func<> type.
As to what the difference is between these two pieces of code, we execute the code separately and look at the resulting SQL statement in SQL Profiler:
First paragraph of the Code effect:
Although we queried with two statements, we eventually generated only one SQL statement, merging the query parameters.
Second code effect:
This time we still see only one SQL statement, but there is only one query condition, but the result of two queries is consistent.
The reason for this is that func<> is compiled into IL code directly by the compiler, but expression<> just stores an expression tree, which is processed at run time, and LINQ to SQL eventually turns the expression tree into the appropriate SQL statement and executes it in the database.
Now we should know when to use Ienumerable<t>, and when to use iqueryable<t>.
Ienumerable<t> and iqueryable<t> distinguish