对象集合中的唯一值 [英] unique Value from the object collection

查看:68
本文介绍了对象集合中的唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个月份和年份的对象集合.

I have an object collection of month and year.

How can I take unique month and year of that object collection?

推荐答案

取决于手头的任务.

您要从列表框中选择吗?

匹配以前提供的选项吗?

有关您已完成的操作和上下文的详细信息,请参见.
Depends on the task at hand.

Are you wanting to select from a listbox?

Match a previously supplied option?

More information regarding what you have done and what the context is please.


您可以尝试这种方式,但是我不确定您将获得什么样的结果显然将两个单独的属性与集合中的对象进行比较.

You can try it this way, but I''m not entirely sure what the outcome will be since you''re evidently comparing two separate properties withiong the object in the 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.


这篇关于对象集合中的唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆