如何根据子集合属性对集合进行排序
我想根据子集合属性对集合进行排序。
//the subcollection
public class Salary
{
public int SalaryId {get;set;}
public int SalaryYear {get;set;}
public double SalaryValue {get;set;} //this is the field we want to sort the parent collection "Person"
}
//the main collection
public class Person
{
public int PersonId {get;set;}
public string PersonName {get;set;}
public List<Salary> Salaries {get;set;}
}
下面仅出于测试目的,我正在准备人员集合,其中每个人员都有 Salaries 内部集合:
List<Person> people = new List<Person>();
//add two salaries for Junior
people.Add(new Person { PersonId = 1, PersonName = "Junior" });
people[0].Salaries.Add(new Salary { SalaryId=1, SalaryYear=2011, SalaryValue=80000 });
people[0].Salaries.Add(new Salary { SalaryId=2, SalaryYear=2010, SalaryValue=70000 });
//add two salaries for Johanna
people.Add(new Person { PersonId = 2, PersonName = "Johanna" });
people[0].Salaries.Add(new Salary { SalaryId=3, SalaryYear=2011, SalaryValue=40000 });
people[0].Salaries.Add(new Salary { SalaryId=4, SalaryYear=2010, SalaryValue=30000 });
现在我们要对人员集合进行排序,但使用其内部集合 SalaryValue 作为参数。
如何对列表进行排序,但在 Salaries 内部集合上使用 LINQ / Lambda 表达式?
所以我会:
PersonName: Johanna, SalaryValue=30000, SalaryYear=2010
PersonName: Johanna, SalaryValue=40000, SalaryYear=2011
PersonName: Junior, SalaryValue=70000, SalaryYear=2010
PersonName: Junior, SalaryValue=80000, SalaryYear=2011
I would like to sort a collection based on a subcollection property.
//the subcollection
public class Salary
{
public int SalaryId {get;set;}
public int SalaryYear {get;set;}
public double SalaryValue {get;set;} //this is the field we want to sort the parent collection "Person"
}
//the main collection
public class Person
{
public int PersonId {get;set;}
public string PersonName {get;set;}
public List<Salary> Salaries {get;set;}
}
Below just for testing purpose, I'm preparing my collection of person with Salaries inner collections each one:
List<Person> people = new List<Person>();
//add two salaries for Junior
people.Add(new Person { PersonId = 1, PersonName = "Junior" });
people[0].Salaries.Add(new Salary { SalaryId=1, SalaryYear=2011, SalaryValue=80000 });
people[0].Salaries.Add(new Salary { SalaryId=2, SalaryYear=2010, SalaryValue=70000 });
//add two salaries for Johanna
people.Add(new Person { PersonId = 2, PersonName = "Johanna" });
people[0].Salaries.Add(new Salary { SalaryId=3, SalaryYear=2011, SalaryValue=40000 });
people[0].Salaries.Add(new Salary { SalaryId=4, SalaryYear=2010, SalaryValue=30000 });
Now we want to sort the people collection, but using their inner collection SalaryValue as parameter.
How can I sort List but using LINQ / Lambda expressions on Salaries inner collection?
So I would have:
PersonName: Johanna, SalaryValue=30000, SalaryYear=2010
PersonName: Johanna, SalaryValue=40000, SalaryYear=2011
PersonName: Junior, SalaryValue=70000, SalaryYear=2010
PersonName: Junior, SalaryValue=80000, SalaryYear=2011
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我来说,这看起来像:
请注意,您并不是真正对人的集合进行排序 - 您正在对(人,薪水)的集合进行排序,这就是拥有两个<的扁平化效果。 code>from 子句确实如此。
(上面不会提供完全相同的输出,但是一旦你得到了这个人和他们的薪水,你就可以得到其他值。)
To me, that looks like:
Note that you're not really sorting a collection of people - you're sorting a collection of (person, salary), which is what the flattening effect of having two
from
clauses does.(The above won't provide exactly the same output, but once you've got the person and their salary, you can get at the other values.)
看起来 Jon 的逻辑是正确的,但示例代码与 OP 不匹配。它可能应该更像是这样的:
It looks like Jon's logic is correct, but the sample code doesn't match the OP's. It should probably be more like this: