1

I'm writing some kind of querybuilder and I need your help.

I have an OrderBy class with 2 Properties:

public class Order
{
    public string OrderColumn { get; set; }

    private bool _sortDesc = false;

    public bool SortDesc
    {
        get { return _sortDesc; }
        set { _sortDesc = value; }
    }
}

Then I put some of these in a collection:

ObservableCollection<Order> OrderBys = new ObservableCollection<Order>()
{
     new Order () {OrderColumn = "Nbr", SortDesc = false},
     new Order () {OrderColumn = "Name", SortDesc = true}
}

Then I have my EntityCollection where I want to apply the orderBys:

ObservableCollection<MyEntity> Entities = EntitiesFormAnyWhere;

foreach (var orderBy in OrderBys)
{
    This is where the magic should happen.
}

Any ideas?

EDIT:

public class MyEntity
{
    public int Nbr {get;set;}
    public string Name {get;set;}
    public string Version {get;set;}
    public string X {get;set}
}

I want to do something like this:

Entities.OrderBy(e => e.Nbr)
        .ThenByDescending(e => e.Name)

But in an dynamic way.

P.S: Is this really a duplicate?

reiniX86
  • 199
  • 2
  • 11
  • https://stackoverflow.com/questions/7265186/how-do-i-specify-the-linq-orderby-argument-dynamically – Rotem Jul 24 '17 at 13:14
  • You sort whole collection in desc or asc order. How did you want to sort each element? Or what do you want to take? – Win4ster Jul 24 '17 at 13:14
  • How does `MyEntity` and `Order` rely each other? Do you search for dynamic sorting based on the name of a property? – MakePeaceGreatAgain Jul 24 '17 at 13:16
  • Yes. I have updated my question to be point this out more clearly. – reiniX86 Jul 24 '17 at 13:29
  • I see no good reason to open this question. All the information you need is in the dupe, you just need to apply it to your particular scenario. If you get stuck and have a **specific** question then come back. I'd also argue this lacks effort as it stands – Liam Jul 24 '17 at 13:44
  • Also you can take a look at the custom extension method here https://stackoverflow.com/questions/36298868/how-to-dynamically-order-by-certain-entity-properties-in-entityframework-7-core/36303246#36303246 - all you need is to replace the `SortModel` class/properties with your `Order` class/properties. – Ivan Stoev Jul 24 '17 at 15:34

0 Answers0