【发布时间】: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列的用途是什么? -
默认排序顺序。但是最终用户可以请求以不同的顺序按孩子的名字降序查看父母......
-
嗯,明白了!也添加了我的答案:)