System.Linq.Dynamic does not support OrderByDescending("someColumn")?

15,596

Solution 1

Assuming that you are using the DynamicQuery Helper files from the Microsoft sample library (Which are in the namespace System.Linq.Dynamic) then after reading the source code it looks like you need to specify the ordering you want as follows:

myDataSource.OrderBy("someColumnName descending")

Solution 2

If you're using string values (like me) you'll have to concatenate it to the string like this:

myDataSource.OrderBy(columnName + " descending");

Don't forget to add a space before 'descending' otherwise you'll get an error.

Share:
15,596
Marko
Author by

Marko

Updated on June 19, 2022

Comments

  • Marko
    Marko almost 2 years

    Ok so in our project I'm using System.Linq.Dynamic library but I just noticed that I cannot do the following:

    myDataSource.OrderByDescending("someColumnName")
    

    Because I get the following error:

    Overload resolution failed because no accessible OrderByDescending can be called with these arguments...

    It seems that the Library only support OrderBy("someColumnName"). Is there a reason for this and how would I bypass this issue if I want to reorder the records in descending order? Do I have to user Reverse() for example OrderBy("someColumnName").Reverse()? Seems like a hack...

    Any advice would be greatly appreciated...

  • Sebastian Siek
    Sebastian Siek about 12 years
    I thought that OrderByDescending method does not accept only 1 string parameter ?!
  • Leom Burke
    Leom Burke about 12 years
    Sorry - I mistyped the answer. The OrderBy method in the DynamicQuery helper from Microsoft allows you to specify comma-separated string orderings such as this.
  • Marko
    Marko about 12 years
    Yes this was the answer I was looking for. If I might add you can actually just say myDataSource.OrderBy("someColumnName DESC") in other words you don't have to type the entire descending word.
  • Pinte Dani
    Pinte Dani about 5 years
    Works, although not intuitive at all from the Framework design. It would have been more clear if System.Linq.Dynamic would contain also a method for OrderByDescending which accepts only a string parameter, like OrderBy