【问题标题】:Sort List of Parent / Child Objects父/子对象的排序列表
【发布时间】:2014-11-29 13:52:58
【问题描述】:

我在弄清楚如何根据子项有效地对父项列表进行排序时遇到了一些麻烦。

我不能只对子项进行排序。我需要子项排序的结果来影响父列表的排序。

基本上我想要做的是按照反映孩子名字降序的顺序对父母进行排序。

一旦我已经在内存中有一个父母列表,是否有一种“linqish”的方式来做到这一点?如果是这样,您能负担得起的任何帮助都会很棒。

这是一个例子......

//What I am trying to do is to figure out how to sort the order of parent1, parent2, parent3 
//based on the names of their children. 
//More specifically, the expected output would be:
//parent 1 (because she has a child with the name of Zoey), 
//parent 3 (because she has a child next in desc order with the name of Yolanda), 
//parent 2 (because her child names in desc order would Matt).

public class Parent
{
    public int id { get; set; }
    public int SortOrder { get; set; }
    //some properties
    public List<Child> Children { get; set; }

    public static List<Parent> GetSortedParentsByChildName()
    {
        List<Parent> myUnsortedList = new List<Parent>()
        {
            new Parent()
            {
                id = 1,
                Children = new List<Child>()
                {
                    new Child(1, "Billy"),
                    new Child(1, "Zoey"),
                    new Child(1, "Robert"),
                }
            },
            new Parent()
            {
                id = 2,
                Children = new List<Child>()
                {
                    new Child(1, "Gabe"),
                    new Child(1, "Matt"),
                    new Child(1, "Alyssa"),
                }
            },
            new Parent()
            {
                id = 3,
                Children = new List<Child>()
                {
                    new Child(1, "Will"),
                    new Child(1, "Bob"),
                    new Child(1, "Yolanda"),
                }
            },
        };


        return myUnsortedList; //.OrderBy(my actual question);
    }
}

public class Child
{
    public int id { get; set; }
    //some properties
    public string Name { get; set; }

    public Child(int id, string Name)
    {
        this.id = id;
        this.Name = Name;

    }
}

【问题讨论】:

  • 您期望的任何示例输入\输出?
  • List mySortedParents = ?????一些神奇的方法可以按孩子的名字降序对所有父母进行排序???
  • SortOrder 列的用途是什么?
  • 默认排序顺序。但是最终用户可以请求以不同的顺序按孩子的名字降序查看父母......
  • 嗯,明白了!也添加了我的答案:)

标签: linq sorting


【解决方案1】:

好的,你也可以这样做:-

List<Parent> mySortedList =
    myUnsortedList
        .OrderByDescending(
            x => x.Children.OrderByDescending(z => z.Name).First().Name)
        .ToList();

【讨论】:

  • 如果你拿出[0] 我会说这是一个很好的答案。
  • @Enigmativity - 是的.. 我也这么想 :) 现在好点了吗?
  • 我觉得不错。我投了赞成票,我认为你应该得到接受的答案。
  • 这段代码也可以工作,我认为它也更容易阅读。感谢您的帮助。
【解决方案2】:

这对我来说可以替换 GetSortedParentsByChildName 中的 return 行:

        var childrenMap =
            myUnsortedList
                .SelectMany(x => x.Children)
                .Select(x => x.Name)
                .Distinct()
                .OrderBy(n => n)
                .Select((n, i) => new { n, i })
                .ToDictionary(x => x.n, x => x.i);

        return myUnsortedList
            .Select(x => new
            {
                x,
                max = x.Children
                    .Select(y => childrenMap[y.Name])
                    .Max()
            })
            .OrderByDescending(x => x.max)
            .Select(x => x.x)
            .ToList();

我得到这个结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-03
    • 2019-10-08
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多