LINQy检查集合中是否有任何对象具有相同属性值的方法 [英] LINQy way to check if any objects in a collection have the same property value
本文介绍了LINQy检查集合中是否有任何对象具有相同属性值的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个具有属性ID的类Agent
I have a class Agent with a property Id
给出了一组代理,我需要检查其中是否有重复的ID.
Given a collection of Agents I need to check if any of them have duplicate Ids.
我目前正在使用哈希表进行此操作,但是正试图使Linq变得合法化,这是什么好方法?
I am currently doing this with a hash table but am trying to get Linq-ified, what's a good way of doing this?
推荐答案
类似于Y Low的方法,
Similar to Y Low's approach,
已
var duplicates = agents.GroupBy(a => a.ID).Where(a=>a.Count() > 1);
foreach (var agent in duplicates)
{
Console.WriteLine(agent.Key.ToString());
}
这篇关于LINQy检查集合中是否有任何对象具有相同属性值的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文