0

Lets say I have a structure as follows:

class A {
    IEnumerable<B> BList {get;set;}
}

class B {
    IEnumerable<C> CList {get;set};
    int OrderId {get;set;}
}

class C {
    int OrderId {get;set;}
}

and I want to return a list of As where it's BList is ordered by it's OrderId and for each B in that list , it's CList is also ordered by it's OrderId.

These are models I use with EF6, and classes A,B,C correspond to db tables.

I have spent some trying to 'linq-ify' this, but without success. Is it doable?

EDIT: duplicate of Sort a list and all its nested objects using Linq

Community
  • 1
  • 1
Mr Balanikas
  • 1,657
  • 15
  • 28

1 Answers1

2

ok, as nobody wanted to answer and you don't add more here is my take on it for now:

public static A SortEverything(A input)
{
    return new A
    {
        BList = 
            input.BList
            .Select(b =>
                new B
                {
                    OrderId = b.OrderId,
                    CList = b.CList.OrderBy(c => c.OrderId)
                })
            .OrderBy(b => b.OrderId)
    };
}

to return a so sorted list of As having one input list/enumerable IEnumerable<A> input you just have to

var sorted = input.Select(SortEverything);

BTW: there are some issues in your classes - of course you have to make the properties public:

class A {
    public IEnumerable<B> BList {get; set;}
}

class B {
    public IEnumerable<C> CList {get; set; }
    public int OrderId {get;set;}
}

class C {
    public int OrderId {get;set;}
}
Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • BTW, would be nice to see why the question got downvoted. How am I otherwise gonna learn to write proper questions? – Mr Balanikas Jul 31 '14 at 10:12
  • don't think to much about it - that's the way Stackoverflow works ATM - it's really bad in the big categories (like C++,Java,C#) - if you don't give a question where they can do a quick < 1min answer they will downvote you without any comment - ignore it and carry on – Random Dev Jul 31 '14 at 10:31