Get Non-Distinct elements from an IEnumerable

前端 未结 4 1417
心在旅途
心在旅途 2021-02-13 13:18

I have a class called Item. Item has an identifier property called ItemCode which is a string. I would like to get a list of all non-distinct Items in a list of Items.

E

4条回答
  •  有刺的猬
    2021-02-13 14:09

    You might want to try it with group by operator. The idea would be to group them by the ItemCode and taking the groups with more than one member, something like :

    var grouped = from i in itemList
                  group i by i.ItemCode into g
                  select new { Code = g.Key, Items = g };
    
    var result = from g in grouped 
                 where g.Items.Count() > 1;
    

提交回复
热议问题