Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between IOrderedQueryable and IQueryable?

I have this:

    var points = from p in ContextDB.Points
                 orderby p.PointInTime descending
                 where p.InstanceID == instanceId
                 && p.ParentPointID == null
                 && p.PointTypeID == currentPointTypeID
                 select p;

and this:

    var points = from p in ContextDB.Points
                 where p.InstanceID == instanceId
                 && p.ParentPointID == null
                 && p.PointTypeID == currentPointTypeID
                 orderby p.PointInTime descending
                 select p;

While I understand the use of both (and one generates an error later) I don't understand how they are different.

I did see questions like this elsewhere on STO, but I've not garnered what the answer to this question is, I'm afraid.

like image 556
Matt W Avatar asked Aug 17 '10 11:08

Matt W


People also ask

What is IOrderedQueryable?

The IOrderedQueryable<T> interface is intended for implementation by query providers. This interface represents the result of a sorting query that calls the method(s) OrderBy, OrderByDescending, ThenBy or ThenByDescending.

What is difference between IQueryable and IEnumerable?

Both IEnumerable and IQueryable are forward collection. Querying data from a database, IEnumerable execute a select query on the server side, load data in-memory on a client-side and then filter data. Querying data from a database, IQueryable execute the select query on the server side with all filters.

What is IEnumerable and IQueryable in LINQ?

In LINQ to query data from database and collections, we use IEnumerable and IQueryable for data manipulation. IEnumerable is inherited by IQueryable, Hence IQueryable has all the features of IEnumerable and except this, it has its own features. Both have its own importance to query data and data manipulation.

Does IEnumerable execute query?

IEnumerable is suitable for querying data from in-memory collections like List, Array and so on. While querying data from the database, IEnumerable executes "select query" on the server-side, loads data in-memory on the client-side and then filters the data.


2 Answers

A type implementing IOrderedQueryable<T> contains extra state to hold information about sorting.

IQueryable<T> normally represents an operation that will be performed later (and possibly in a completely different language on a different computer, e.g. with LINQ to SQL). A separate interface is needed because the next operation might be another sort, which needs to be treated differently to the first (to sort by column A and then B requires two LINQ operators to define, but the system needs to ensure that each key contributes to the overall sort).

like image 135
Richard Avatar answered Sep 23 '22 01:09

Richard


It's easy to see if you translate the query comprehension to its corresponding Linq extension methods. The return type of OrderBy() is IOrderedEnumerable<>. Where() returns IEnumerable<>. What your first expression does is first sort all of the Points, then select only the ones that match the where clause. The last operation applied is Where, thus the expression type is IEnumerable<>.

Which one is more efficient depends largely on the Linq provider you use. My money is on sorting last. Although I'd guess that the provider is smart enough to order the operations in the best order. You might want to check that.

like image 44
Hans Passant Avatar answered Sep 24 '22 01:09

Hans Passant