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

后端 未结 6 583
庸人自扰
庸人自扰 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:27

    foreach(var agent in Agents) {
        if(Agents.Count(a => a.ID == agent.ID) > 1)
            Console.WriteLine("Found: {0}", agent.ID);
    }
    

提交回复
热议问题