简体   繁体   English

使用c#Lambda表达式对对象内的嵌套列表进行排序

[英]Sorting nested Lists within object using c# Lambda Expressions

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. 1)我想使用lambda表达式对该对象进行排序。
2) I would like to get back the object sorted by the first in Asc order 2)我想找回按升序排列的对象
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 3)我想删除两个嵌套的List <>对象中的最后一个属性,所以只有
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); 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. 在此先感谢您的帮助,我似乎无法达到预期的效果,并且我知道自己缺少明显的东西。

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. 如果我理解您的意思,则想通过NestedObject属性在MyOrderList中对对象进行排序,NestedObject是对象的集合,这些对象本身也应该通过AnotherNestedObject的值(也就是列表)进行排序。

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. 首先,您需要实现一个Icomparer或类似的东西,以设置使一个列表大于另一个列表的规则。

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. 好的,我想我更好地理解了您的问题,您只想按int对外部列表对象进行排序,然后也对每个元素内部的列表进行排序。

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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM