简体   繁体   English

C#列表比较忽略类项

[英]C# List comparison ignoring class item

I have two databases DB1 and DB2 where I am retrieving sales orders from each and into lists. 我有两个数据库DB1和DB2,我从中检索销售订单并进入列表。 I now want to find the sales orders that are missing in DB2 that are in DB1 so that they are added to DB2 我现在想要找到DB1中缺少的销售订单,以便将它们添加到DB2中

I have listDB1 and ListDB2 in the form: 我有listDB1形式的listDB1ListDB2

public class SalesOrder
{
      public int docNum;
      public string cardCode;
      public string cardName;
      public DateTime docDate;
      public DateTime docDueDate;                       
      public double docTotal;        
      public int lineItemCount;
      public string comments;
} 

I now want to compare the 2 lists ignoring the docNum for both lists which is auto-generated while comparing the rest of the elements. 我现在想比较两个列表,忽略两个列表的docNum ,这些列表是在比较其余元素时自动生成的。 Using: 使用:

unaddedSOs = listDB1.Except(listDB2).ToList();

compares all including docNum. 比较所有包括docNum。 How do I achieve what I need so I can get the doc Numbers that are unadded? 我如何实现我的需要,以便获得未添加的文档编号?

You can either implement IEquatable<SalesOrder> , or create a custom IEqualityComparer<SalesOrder> . 您可以实现IEquatable<SalesOrder> ,也可以创建自定义IEqualityComparer<SalesOrder> Note i'd also advise you to turn those public fields into properties. 请注意,我还建议您将这些公共字段转换为属性。

public class SalesOrder : IEquatable<SalesOrder>
{
    public int DocNum { get; set; }
    public string CardCode { get; set; }
    public string CardName { get; set; }
    public DateTime DocDate { get; set; }
    public DateTime DocDueDate { get; set; }
    public double DocTotal { get; set; }
    public int LineItemCount { get; set; }
    public string Comments { get; set; }

    public bool Equals(SalesOrder other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return string.Equals(cardCode, other.cardCode) &&
               string.Equals(cardName, other.cardName) &&
               docDueDate.Equals(other.docDueDate) &&
               docTotal.Equals(other.docTotal) && 
               lineItemCount == other.lineItemCount &&
               string.Equals(comments, other.comments);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((SalesOrder) obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            var hashCode = (cardCode != null ? cardCode.GetHashCode() : 0);
            hashCode = (hashCode*397) ^ (cardName != null ? cardName.GetHashCode() : 0);
            hashCode = (hashCode*397) ^ docDueDate.GetHashCode();
            hashCode = (hashCode*397) ^ docTotal.GetHashCode();
            hashCode = (hashCode*397) ^ lineItemCount;
            hashCode = (hashCode*397) ^ (comments != null ? comments.GetHashCode() : 0);
            return hashCode;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM