C# C语言中对象的不同列表#

C# C语言中对象的不同列表#,c#,C#,我必须区分对象列表,但不仅仅是ID,因为有时两个不同的对象具有相同的ID。 我有课: public class MessageDTO { public MessageDTO(MessageDTO a) { this.MsgID = a.MsgID; this.Subject = a.Subject; this.MessageText = a.MessageText; this.ViewedDate = a.View

我必须区分对象列表,但不仅仅是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;
列表示例;
谢谢使用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
    }
}
您还可以通过重写
object.Equals(object)
object.GetHashCode()
并调用
nonDistinct.Distinct()的空重载来避免额外的类。但是,请确保您认识到这个决定的含义:例如,这些将成为所有非显式使用范围内的平等测试函数。这可能是完美的,正是您所需要的,或者它可能会导致一些意想不到的后果。只需确保您知道您将进入什么环境。

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

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

您可以使用与MoreLinq库不同的扩展方法:

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

请参阅:

您可以使用与MoreLinq库不同的扩展方法:

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

请参阅:

如果要使用其他属性,应实现
IEqualityComparer
接口。更多关于:

您还可以在
消息中覆盖
等于
,以
类:

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();

如果要使用其他属性,则应实现
IEqualityComparer
接口。更多关于:

您还可以在
消息中覆盖
等于
,以
类:

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();

我建议您使用@Matthew Haugen的解决方案

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

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

我建议您使用@Matthew Haugen的解决方案

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

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

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