【问题标题】:Distinct List of object in C#C#中对象的不同列表
【发布时间】:2014-07-23 14:04:31
【问题描述】:

我必须区分对象列表,但不仅仅是通过 ID,因为有时两个不同的对象具有相同的 ID。 我有课:

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; }
}

我如何区分以下列表:

List<MessageDTO> example;

谢谢

【问题讨论】:

  • 我必须删除具有相同所有属性的重复对象的重复对象。例如 MsgID、Subject、MessageText、ViewedDate 和 CreateDate。如果某些属性不同,则该对象必须保留在列表中。

标签: c#


【解决方案1】:

使用 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
    }
}

然后

return nonDistinct.Distinct(new MessageDTOEqualityComparer());

您还可以通过覆盖object.Equals(object)object.GetHashCode() 并调用nonDistinct.Distinct() 的空重载来避免需要额外的类。不过,请确保您认识到此决定的含义:例如,这些将成为其使用的所有非显式范围内的相等测试函数。这可能是完美的并且正是您所需要的,或者它可能会导致一些意想不到的后果。只要确保你知道你要做什么。

【讨论】:

    【解决方案2】:

    如果你想使用其他属性,你应该实现IEqualityComparer接口。更多信息:msdn

    class MsgComparer : IEqualityComparer<MessageDTO>
    {
        public bool Equals(MessageDTO x, MessageDTO Oy)
        {
        }
    
        // If Equals() returns true for a pair of objects 
        // then GetHashCode() must return the same value for these objects.
    
        public int GetHashCode(MessageDTO m)
        {
               //it must br overwritten also
        }
    
    }
    

    然后:

    example.Distinct(new MsgComparer());
    

    您还可以在MessageDTO 类中覆盖 Equals

    class MessageDTO 
    {
        // rest of members
        public override bool Equals(object obj) 
        {
            // your stuff. See: http://msdn.microsoft.com/en-us/library/ms173147%28v=vs.80%29.aspx
        }
        public override int GetHashCode()
        {
        }
    }
    

    那就够了:

    example.Distinct();
    

    【讨论】:

      【解决方案3】:

      您可以使用 MoreLinq 库中的扩展方法 DistinctBy:

      string[] source = { "first", "second", "third", "fourth", "fifth" };
      var distinct = source.DistinctBy(word => word.Length);
      

      here:

      【讨论】:

        【解决方案4】:

        我推荐你使用@Matthew Haugen 的解决方案

        如果您不想为此创建新类,有一种方法可以使用 LINQ,方法是按不同的字段对列表进行分组,然后选择该组中的第一项。例如:

        example.(e => new { e.MsgID, e.Subject }).Select(grp => grp.FirstOrDefault());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-05
          • 1970-01-01
          • 2013-06-07
          相关资源
          最近更新 更多