George Mauer George Mauer - 7 months ago 34
C# Question

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

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 table but am trying to get Linq-ified, what's a good way of doing this?


Similar to Y Low's approach,


 var duplicates = agents.GroupBy(a => a.ID).Where(a=>a.Count() > 1);

 foreach (var agent in duplicates)