George Mauer George Mauer - 1 year ago 95
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?

Answer Source

Similar to Y Low's approach,


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

 foreach (var agent in duplicates)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download