Using LINQ to find duplicates across multiple properties

前端 未结 3 1188
滥情空心
滥情空心 2020-12-31 00:22

Given a class with the following definition:

public class MyTestClass
{
    public int ValueA { get; set; }
    public int ValueB { get; set; }
}
         


        
3条回答
  •  我在风中等你
    2020-12-31 01:21

    You can find your duplicates by grouping your elements by ValueA and ValueB. Do a count on them afterwards and you will find which ones are duplicates.

    This is how you would isolate the dupes :

    var duplicates = items.GroupBy(i => new {i.ValueA, i.ValueB})
      .Where(g => g.Count() > 1)
      .Select(g => g.Key);
    

提交回复
热议问题