gpt4 book ai didi

c# - LINQ GroupBy - 按名称选择最低 ID 组

转载 作者:太空宇宙 更新时间:2023-11-03 19:52:52 27 4
gpt4 key购买 nike

我想弄清楚如果名称相同,如何只选择 ID 最低的对象:

public class BasicInfo
{
public int Id {get;set;}
public string Name {get; set;}
}

List<BasicInfo> BasicInfos = new List<BasicInfo>();
BasicInfos.Add(new BasicInfo() { Id = 1, Name = "John" });
BasicInfos.Add(new BasicInfo() { Id = 2, Name = "John" });

BasicInfos = BasicInfos.GroupBy(y => y.Name)...(What goes here?)

最佳答案

类似的东西:

BasicInfos = BasicInfos.GroupBy(y => y.Name)
.Select(y => new BasicInfo{ Name = y.Key, Id = y.Min(x => x.Id));

这将为您提供所有具有最低键的名称。

关于c# - LINQ GroupBy - 按名称选择最低 ID 组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36633844/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com