0

I have an object

        TestCollection testCollection = new TestCollection()
        {
           new Test
           {
             quickTest = 
                new QuickTest
                {
                    id = a,
                    sortField = 3,
                    nestedQuickTest = 
                        new List<NestedQuickTest>
                        {
                            new NestedQuickTest{sortField = 1},
                            new NestedQuickTest{sortField = 2},
                            new NestedQuickTest{sortField = 3},
                            new NestedQuickTest{sortField = 4},
                        }
                }
           },

           new Test
           {
             quickTest = 
                new QuickTest
                {
                    id = b,
                    sortField = 2,
                    nestedQuickTest = 
                        new List<NestedQuickTest>
                        {
                            new NestedQuickTest{sortField = 21},
                            new NestedQuickTest{sortField = 32},
                            new NestedQuickTest{sortField = 11},
                            new NestedQuickTest{sortField = 2},
                        }
                }
           },

           new Test
           {
             quickTest = 
                new QuickTest
                {
                    id = c,
                    sortField = 1,
                    nestedQuickTest = 
                        new List<NestedQuickTest>
                        {
                            new NestedQuickTest{sortField = 3},
                            new NestedQuickTest{sortField = 2},
                            new NestedQuickTest{sortField = 8},
                            new NestedQuickTest{sortField = 1},
                        }
                }
           },
        };

1) I would like to sort this object using lambda expressions.
2) I would like to get back the object sorted by the first in Asc order
and then by the in Asc order.
3) I would like to remove the last property in both nested List<> objects so there are only
two objects in each.

I apologize if this is a little confusing but I would like to do something like this:

var sorted = testCollection.OrderBy(x => x.quickTest.sortField).ThenBy(y => y.quickTest.nestedQuickTest.Select(z => z.sortField)).Take(2);


The end result would be:

        TestCollection testCollection = new TestCollection()
        {

,

           new Test
           {
             quickTest = 
                new QuickTest
                {
                    id = c,
                    sortField = 1,
                    nestedQuickTest = 
                        new List<NestedQuickTest>
                        {
                            new NestedQuickTest{sortField = 1},
                            new NestedQuickTest{sortField = 2}
                        }
                }
           },

           new Test
           {
             quickTest = 
                new QuickTest
                {
                    id = b,
                    sortField = 2,
                    nestedQuickTest = 
                        new List<NestedQuickTest>
                        {
                            new NestedQuickTest{sortField = 2},
                            new NestedQuickTest{sortField = 11}
                        }
                }
           }
        };

Thanks in advance for your help, I can't seem to get my desired results and I know I am missing something obvious.

4
  • Please format your code with the code block so we get syntax coloring :) Commented Feb 16, 2011 at 17:59
  • Did you mean List<MyObject> instead of LIST<"MyObject">? Also, your Object Initializer syntax has no sense, there cannot be more than one property with the name "NestedObject". Can you please provide short, compilable code? Commented Feb 16, 2011 at 18:06
  • What results are you getting instead? It might also help to show something closer to the actual code—the way you have the code now is kind of confusing (e.g., is "NestedObject=3" supposed to be a list item?). Commented Feb 16, 2011 at 18:14
  • Thanks for the quick reply's. I have updated my code and hopefully it will give a clearer picture of what I am trying to accomplish. Commented Feb 16, 2011 at 19:06

1 Answer 1

1

If I understand what you are saying you want to order the objects in MyOrderList by the property with the NestedObject, which is a collection of objects who should themselves ordered by the value of the AnotherNestedObject, which is also a list.

Well first of all you need to implement a Icomparer or something like that to set the rules of what makes one list bigger than the other.

See, you want to order objects in a list by a value, if its an int, well thats easy, but being a list, what are the rules to determine which one is bigger? Is it the one with more elements? The one with biggest sum of elements?

You need to figure out this first

EDIT:

Okay I think I understand your question better, you just want to order the outer lists objects by the int, and then also have the list inside each of those elements ordered too.

I think it would be easier if you did this in 2 steps something like:

  var orderedTestCollection = testCollection.Orderby(tc=>tc.quickTest.SortField) // orders all the testCollection objects


    //orders and only takes 2 elements of the lists inside the each test collection object
    foreach(var tc in testCollection)
    {
       tc.quickTest.NestedQuickTest = tc.quickTest.NestedQuickTest.Orderby(nqt=>nqt.SortField).Take(2);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Let's say for simplicity I am sorting on an int field. I have updated my code that may give a clearer picture of what I'm trying to accomplish. I am able to sort the first nested objects called "QuickTest" but I am having trouble when trying to drill down into the "NestedQuickTest" objects.
Thanks, I ended up taking your suggestion and did it in two steps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.