0

Suppose I have a sequence of objects that I want to sort using the names of its properties, e.g. sequence IQueryable<T> to be sorted first by property A, then B, then C. I have the following code:

    using System.Linq.Dynamic.Core;

    IQueryable<T> source = ...
    var properties = new[]{"A","B","C"};
    foreach (var property in properties.Reverse())
    {
        source = source.OrderBy(property);
    }
    return source;

The resulting code returns source sorted only by the latest applied property (i.e. A in this case) disregarding all the previous orderings. Is it the expected behavior?

user2341923
  • 4,277
  • 5
  • 27
  • 40
  • 1
    you should use ThenBy on IOrderedEnumerable (which is returned by OrderBy extension), if you want to sort on multiple fields – user7313094 Feb 26 '20 at 18:43
  • 1
    Does this answer your question? [LINQ OrderBy versus ThenBy](https://stackoverflow.com/questions/3760001/linq-orderby-versus-thenby) – user7313094 Feb 26 '20 at 18:44
  • Yes, thank you. `ThenBy` requires `IOrderedQueryable`, that’s why IntelliSence did not see it. – user2341923 Feb 26 '20 at 18:52

0 Answers0