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>
?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
After a lot of searching this worked for me:
Thanks to Maarten (Query a collection using PropertyInfo object in LINQ) I got this solution:
In my case I was working on a "ColumnHeaderMouseClick" (WindowsForm) so just found the specific Column pressed and its correspondent PropertyInfo:
OR
(be sure to have your column Names matching the object Properties)
Cheers
Convert List to IEnumerable or Iquerable, add using System.LINQ.Dynamic namespace, then u can mention the property names in comma seperated string to OrderBy Method which comes by default from System.LINQ.Dynamic.
Use dynamic
linq
just add
using System.Linq.Dynamic;
And use it like this to order all your columns:
I found the answer. I can use the
.AsQueryable<>()
extension method to convert my list to IQueryable, then run the dynamic order by against it.Here's something else I found interesting. If your source is a DataTable, you can use dynamic sorting without using Dynamic Linq
reference: http://msdn.microsoft.com/en-us/library/bb669083.aspx (Using DataSetExtensions)
Here is one more way to do it by converting it to a DataView: