C# LINQ find duplicates in List

前端 未结 9 1202
星月不相逢
星月不相逢 2020-11-22 07:57

Using LINQ, from a List, how can I retrieve a list that contains entries repeated more than once and their values?

9条回答
  •  温柔的废话
    2020-11-22 08:27

    there is an answer but i did not understand why is not working;

    var anyDuplicate = enumerable.GroupBy(x => x.Key).Any(g => g.Count() > 1);
    

    my solution is like that in this situation;

    var duplicates = model.list
                        .GroupBy(s => s.SAME_ID)
                        .Where(g => g.Count() > 1).Count() > 0;
    if(duplicates) {
        doSomething();
    }
    

提交回复
热议问题