Get Non-Distinct elements from an IEnumerable

前端 未结 4 1459
心在旅途
心在旅途 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 13:54

    as an extension method:

    public static IEnumerable NonDistinct (this IEnumerable source, Func keySelector)
    {
       return source.GroupBy(keySelector).Where(g => g.Count() > 1).SelectMany(r => r);
    }
    

提交回复
热议问题