Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting IQueryable to IOrderedQueryable generically

I have a generic method where I take an IQueryable<T> and returns an IOrderedQuerable<T> using Linq-to-Entities.

A simple input.OrderBy(p => p.something) won't work since I don't know any property of T (and I cannot constrain this to an interface).

Casting the result to (IOrderedQuerable<T>) seems to work until you try do actually use it with a .Skip() or .Take(), at which point you get a runtime error.

I guess I theoretically could use reflection and see if I find an int or something and build an expression to use as ordering, but that seems very dirty.

Any ideas?

like image 564
Toodleey Avatar asked Jan 25 '13 15:01

Toodleey


1 Answers

input.OrderBy(p => 0);

This way you'll have the items in the same order they were initially. However, this will cost extra CPU.

like image 187
RePierre Avatar answered Oct 24 '22 14:10

RePierre