C#怎么获取 List集合中的重复值Linq操作
2023-11-15

List查询重复数据

var data = new List<DataDto>
{
    new() { Id = 1, Name = "张一", Age = 100 },
    new() { Id = 2, Name = "张三", Age = 100 },
    new() { Id = 3, Name = "张三", Age = 100 },
    new() { Id = 5, Name = "张三", Age = 100 },
    new() { Id = 4, Name = "张四", Age = 100 },
};


var result = data.Where(x => x.Age == 100)
    .GroupBy(x => x.Name)
    .Where(x => x.Count() > 1)
    .ToList();

foreach (var dataDto in result)
{
    foreach (var item in dataDto)
    {
        Console.WriteLine($"{item.Name}{item.Id}");
    }
}

internal class DataDto
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

参考:https://www.cnblogs.com/fps2tao/p/16355863.html
https://blog.csdn.net/xiaochenXIHUA/article/details/107020130