侧边栏壁纸
博主头像
小医仙博主等级

风一程雨一程,身向榆关那畔行。

  • 累计撰写 39 篇文章
  • 累计创建 16 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

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

小医仙
2022-08-30 / 0 评论 / 0 点赞 / 555 阅读 / 141 字

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

评论区