在C#中的对象不同清单 [英] Distinct List of object in C#

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

问题描述

我要的对象不同的名单,但不仅ID,因为有时候两个不同的对象有相同的ID。
我有类:

 公共类MessageDTO 
{

公共MessageDTO (MessageDTO一)
{
this.MsgID = a.MsgID;
this.Subject = a.Subject;
this.MessageText = a.MessageText;
this.ViewedDate = a.ViewedDate;
this.CreatedDate = a.CreatedDate;
}

公众诠释? {的MsgID获得;组; }
公共字符串主题{搞定;组; }
公共字符串MessageText中获得{;组; }
公共System.DateTime的? ViewedDate {搞定;组; }
公共System.DateTime的? CreatedDate {搞定;组; }
}



我怎么可以在不同的列表:

 列表< MessageDTO>例; 



感谢


解决方案

使用LINQ

 公共类MessageDTOEqualityComparer:EqualityComparer< MessageDTO> 
{
公共布尔等于(MessageDTO一,MessageDTO B)
{
//你的逻辑,它会检查每个消息的属性什么
//理由需要认为他们平等。根据你的情况,这听起来像
//这将仅仅是通过每个属性与
//如果 - 不等于回报假块,然后在最后$返回true迭代的问题b $ b}

公众诠释的GetHashCode(MessageDTO消息)
{
//你的逻辑,我可能只返回信息ID如果可以的话,
//假设不重叠太多,它确实
//必须相等的两个
}
}

然后

 返回nonDistinct.Distinct(新MessageDTOEqualityComparer() ); 

您也可避免一个额外类需要通过重写的Object.Equals (对象) object.GetHashCode()并调用 nonDistinct.Distinct的)空过载(。确保你认识到这个决定的影响,但:例如,那些便会成为平等的测试功能的所有的不明确其使用范围的。这可能是完美的,正是你需要什么,或者它可能会导致一些意想不到的后果。只要确保你知道你要成什么样。


I have to distinct list of object but NOT only by ID because sometimes two different objects have same ID. I have class:

public class MessageDTO
{

    public MessageDTO(MessageDTO a)
    {
        this.MsgID = a.MsgID;
        this.Subject = a.Subject;
        this.MessageText = a.MessageText;
        this.ViewedDate = a.ViewedDate;
        this.CreatedDate = a.CreatedDate;
    }

    public int? MsgID { get; set; }
    public string Subject { get; set; }
    public string MessageText { get; set; }
    public System.DateTime? ViewedDate { get; set; }
    public System.DateTime? CreatedDate { get; set; }
}

How I can distinct list of:

List<MessageDTO> example;

Thanks

解决方案

Use LINQ.

public class MessageDTOEqualityComparer : EqualityComparer<MessageDTO>
{
    public bool Equals(MessageDTO a, MessageDTO b)
    {
        // your logic, which checks each messages properties for whatever
        // grounds you need to deem them "equal." In your case, it sounds like
        // this will just be a matter of iterating through each property with an
        // if-not-equal-return-false block, then returning true at the end
    }

    public int GetHashCode(MessageDTO message)
    {
        // your logic, I'd probably just return the message ID if you can,
        // assuming that doesn't overlap too much and that it does
        // have to be equal on the two
    }
}

Then

return nonDistinct.Distinct(new MessageDTOEqualityComparer());

You can also avoid the need for an extra class by overriding object.Equals(object) and object.GetHashCode() and calling the empty overload of nonDistinct.Distinct(). Make sure you recognize the implications of this decision, though: for instance, those will then become the equality-testing functions in all non-explicit scopes of their use. This might be perfect and exactly what you need, or it could lead to some unexpected consequences. Just make sure you know what you're getting into.

这篇关于在C#中的对象不同清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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