确定是否在列表的两个或多个对象根据一些属性是相等 [英] Determine whether two or more objects in a list are equal according to some property
本文介绍了确定是否在列表的两个或多个对象根据一些属性是相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个List 名单,其中,为MyObject> myObjectList
。该为MyObject
对象有一个名为订单
这类型的属性 INT
。我怎样才能确定 myObjectList
两个或多个对象是否具有相同的订单
使用LINQ到对象?
Say I have a list List<MyObject> myObjectList
. The MyObject
object has a property named Order
which is of type int
. How can I determine whether two or more objects in myObjectList
have the same Order
using LINQ-to-objects?
推荐答案
首先<一href="http://msdn.microsoft.com/en-us/library/system.linq.enumerable.groupby.aspx"><$c$c>GroupBy$c$c> MyObject.Order
然后再确定是否的 任何
的群体有一个以上的成员:
First GroupBy
MyObject.Order
and then determine if Any
of the groups have more than one member:
bool b = myObjectList.GroupBy(x => x.Order)
.Any(g => g.Count() > 1);
// b is true is there are at least two objects with the same Order
// b is false otherwise
这篇关于确定是否在列表的两个或多个对象根据一些属性是相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文