Alex Rivera | Logout

What's the difference between IOrderedQueryable and IQueryable?

Asked 2010-08-17T11:26:19.543
10

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.

Edit
Report

1 Answer

6

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).

answered 2010-08-17T11:48:04.093

Your Answer