我有一个person
列表,其中person
包含weight
和age
属性。
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提取方法中做到这一点?
答案 0 :(得分:1)
您可以像这样使用GroupBy
:
var GroupedPersonList = personList.GroupBy(x => x.Age , (key,element) => new
{
Age = key,
Weight = element.Max(c=>c.Weight)
});
答案 1 :(得分:0)
......或者喜欢
ListView<Label> list = new ListView<Label>();
ObservableList<Label> data = FXCollections.observableArrayList(
new Label("123"), new Label("45678999"));
@Override
public void start(Stage stage) {
VBox box = new VBox();
Scene scene = new Scene(box, 200, 200);
stage.setScene(scene);
stage.setTitle("ListViewExample");
box.getChildren().addAll(list);
VBox.setVgrow(list, Priority.ALWAYS);
list.setItems(data);
list.setCellFactory(new Callback<ListView<Label>, ListCell<Label>>() {
@Override
public ListCell<Label> call(ListView<Label> list) {
ListCell<Label> cell = new ListCell<Label>() {
@Override
public void updateItem(Label item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setItem(item);
}
}
};
return cell;
}
}
);
stage.show();
}