1

次の方法がある場合:

public bool Equals(VehicleClaim x, VehicleClaim y)
{           
    bool isDateMatching = this.IsDateRangeMatching(x.ClaimDate, y.ClaimDate);
    // other properties use exact matching
}

private bool IsDateRangeMatching(DateTime x, DateTime y)
{       
    return x >= y.AddDays(-7) && x <= y.AddDays(7);           
}

値が正確に一致する場合はオーバーライドできますGetHashcodeが、一致がこのような範囲内にある場合はどうすればよいかわかりません。

誰でも助けてもらえますか?

4

2 に答える 2

2

I agree with Chris that

But if you have Date1 as Oct1, Date2 as Oct6, Date3 as Oct12. Date1 == Date2, and Date2 == Date3, but Date1 != Date3 is a strange behavior and the Equals purpose is not what you should use.

I don't really know if you are developping something new and if you have a database containing your vehicule claims, but I would have associated claims in the database as parent child relationship. What you have to ask yourself is: Is it possible that 2 vehicule claims within your date range can be different? Is there any other property you can use to identify a claim as unique?

Maybe you could resolve your problem this way:

Having a

List<VehicleClaim> RelatedClaims 

property in your VehicleClaim object.

Instead of Equals, use a function

bool IsRelated(VehicleClaim vehiculeClaim)
{ 
  if(all properties except date are equals)
  {  
     // since claims are sorted by date, we could compare only with the last element
     foreach(var c in RelatedClaims){ 
        if (IsDateRangeMatching(this.ClaimDate, c.ClaimDate)) 
           return true;
     }
  }

  return false;
}

and add code to construct your object

List<VehiculeClaim> yourListWithDuplicatesSortedByDate;
List<VehiculeClaim> noDuplicateList = new List<VehiculeClaim>();
foreach(var cwd in yourListWithDuplicatesSortedByDate)
{
  var relatedFound = noDuplicateList.FirstOrDefault(e => e.IsRelated(cwd));

  if (relatedFound != null)
    relatedFound.RelatedClaims.Add(cwd);
  else
    noDuplicateList.Add(cwd);
}

Problem with this, your claims must be sorted by date and it could not be the most efficient way to accomplish that.

于 2013-10-03T14:34:32.420 に答える