LINQy way to check if any objects in a collection have the same property value

后端 未结 6 575
庸人自扰
庸人自扰 2020-12-17 00:07

I have a class Agent with a property Id

Given a collection of Agents I need to check if any of them have duplicate Ids.

I am currently doing this with a hash

6条回答
  •  抹茶落季
    2020-12-17 00:42

    this is how i would do it without the need to do group-by in one line:

     List duplicates = new HashSet(agents.Where(c => agents.Count(x => x.ID == c.ID) > 1)).ToList();
    

提交回复
热议问题