gpt4 book ai didi

c# - 从类型类列表中删除重复项

转载 作者:行者123 更新时间:2023-12-02 11:21:32 25 4
gpt4 key购买 nike

我有一个具有以下属性的类

id(类型:唯一的long),name(类型:string),主要版本(VM)(类型:long),次要版本(Vm)(类型:long)

我创建了此类的列表,该列表如下所示

ID    Name     VM    Vm
1 ssim 2 1
2 SSim 3 1
3 Counter 5 1
4 Counter 6 2
5 Counter 6 5

我想根据主要版本和次要版本从列表中删除重复项。最终列表应如下所示

ID    Name     VM    Vm
2 SSim 3 1
5 Counter 6 5

最佳答案

我认为是这样的:

public class Product
{
public Product(long id, string name, int major, int minor)
{
this.Id = id;
this.Name = name;
this.Major = major;
this.Minor = minor;
}

public long Id { get; set; }

public int Major { get; set; }

public int Minor { get; set; }

public string Name { get; set; }
}

private static void Main()
{
IEnumerable<Product> products = new List<Product>
{
new Product(1, "ssim", 2, 1),
new Product(2, "SSim", 3, 1),
new Product(3, "Counter", 5, 1),
new Product(4, "Counter", 6, 2),
new Product(5, "Counter", 6, 5)
};

IEnumerable<Product> distinctProducts =
(from x in products group x by x.Name.ToLower() into g select g.OrderByDescending(y => y.Major).ThenByDescending(y => y.Minor).First()).OrderBy(x => x.Name).ToList();
}

关于c# - 从类型类列表中删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34158470/

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