我有一個person
列表包含person
有一個weight
和age
屬性。如何在Linq提取方法中使用order by和group by?
Person.cs
:
public class Person
{
public int Weight { get; set; }
public int Age { get; set; }
}
Program.cs
:應列出
List<Person> personList = new List<Person>();
personList.Add(new Person { Age = 15, Weight = 68 });
personList.Add(new Person { Age = 19, Weight = 75 });
personList.Add(new Person { Age = 17, Weight = 68 });
personList.Add(new Person { Age = 15, Weight = 55 });
personList.Add(new Person { Age = 15, Weight = 53 });
personList.Add(new Person { Age = 17, Weight = 88 });
personList.Add(new Person { Age = 19, Weight = 45 });
personList.Add(new Person { Age = 20, Weight = 88 });
//Grouped person list
List<People> GroupedPersonList = new List<People>();
GroupedPersonList = personList.OrderBy(x => x.Weight).????
元素:由age
分組,每個組應與最高的權重進行選擇(我要訂購該列表由age
,但該列表僅包含該年齡段的最重人,然後我將通過此創建一個新列表。) 新的新列表應如下所示:
(GroupedPersonList Contents)=>
Age | Weight
-------|-------
15 | 68
17 | 88
19 | 75
20 | 88
我如何能做到這一點的LINQ提取方法?
謝謝你,有一個愉快的一天 –