Using Linq to find duplicates but get the whole record

后端 未结 2 2105
野性不改
野性不改 2021-02-15 00:36

So I am using this code

    var duplicates = mg.GroupBy(i => new { i.addr1, i.addr2 })
                    .Where(g => g.Count() > 1)
                          


        
相关标签:
2条回答
  • 2021-02-15 01:09

    You should use First() instead of Key:

    var duplicates = mg.GroupBy(i => new { i.addr1, i.addr2 })
                    .Where(g => g.Count() > 1)
                    .Select(g => g.First());
    

    It returns the first row of each duplicate groups

    0 讨论(0)
  • 2021-02-15 01:12

    To get all values, you can use ToList() on IGrouping

    var duplicates = mg.GroupBy(i => new { i.addr1, i.addr2 })
                       .Where(g => g.Count() > 1)
                       .Select(g => new {g.Key, Values = g.ToList()});
    
    0 讨论(0)
提交回复
热议问题