views:

263

answers:

2

How to use orderby in IQueryable object?

+1  A: 

You use it like so:

var orderedResult = yourContext.YourTable
        .OrderBy(yt => yt.SomeValueThatYouWantTheResultOrderedBy);
klausbyskov
no i mean i call a function where the Linq to Entities query is written and it returns the IQueryable object.I want to apply orderby and skip and take extension methods to this object.
bbbbb
@bbbbb How is that different? In the above example `yourContext.YourTable` is an IQueryable.
klausbyskov
+1  A: 

From the comment of klausbyskov's answer, your question should be: How do I convert an IOrdereQueryable into an IQueryable object?

In that case, you should convert it to a List object first (which means the query will be executed), and then perform a select on the resulting list:

var orderedList = (
    from q in MyDataContext.MyTable 
    orderby q.SortColumn 
    select q
    ).ToList();
var queryableList = orderedList.Select(q => q);

Note: As I said, the orderedList will be evaluated, so I wouldn't recommend using it for large datasets.

Prutswonder