Casting IQueryable to IOrderedQueryable generically

c#, entity-framework, linq-to-entities

Solution

input.OrderBy(p => 0);

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

Problem

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?

Original source

Related problems