本文介绍了对象集合中的唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个月份和年份的对象集合.
I have an object collection of month and year.
How can I take unique month and year of that object collection?
推荐答案
var list = (from item in collection
select item.).Distinct();
当然,您始终可以实现比较类以传递给Distinct()方法,该方法将以更具体的形式处理比较.
Of course, you can always implement a comparare class to pass to the Distinct() method that will handle the comparison in a more specific anner.
public class MyObjectComparer : IEqualityComparer<myobject>
{
public bool Equals(MyObject x, MyObject y)
{
bool equals = false;
if (x != null && y != null)
{
equals = (x.Month == y.Month && x.Day == y.Day);
}
return equals;
}
}
...
MyObjectComparer comparer = new MyObjectComparer();
var list = (from item in collection
select item.).Distinct(comparer);
上面的代码在C#中,但是将其转换为VB应该没有任何问题.
The code above is in C# but you shouldn''t have any problems converting it to VB.
这篇关于对象集合中的唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!