I found an example in the VS2008 Examples for Dynamic LINQ that allows you to use a SQL-like string (e.g. OrderBy("Name, Age DESC")) for ordering. Unfortunately, the method included only works on IQueryable<T>. Is there any way to get this functionality on IEnumerable<T>?
I found an example in the VS2008 Examples for Dynamic LINQ that allows you
Share
Just stumbled into this oldie…
To do this without the dynamic LINQ library, you just need the code as below. This covers most common scenarios including nested properties.
To get it working with
IEnumerable<T>you could add some wrapper methods that go viaAsQueryable– but the code below is the coreExpressionlogic needed.Edit: it gets more fun if you want to mix that with
dynamic– although note thatdynamiconly applies to LINQ-to-Objects (expression-trees for ORMs etc can’t really representdynamicqueries –MemberExpressiondoesn’t support it). But here’s a way to do it with LINQ-to-Objects. Note that the choice ofHashtableis due to favorable locking semantics: