Introduction
One can use IEquatableString Comparisons
- Handle whitespace between two words , letters.
- Handle trailing and leading spaces
- Handle casing of strings
//Private Member
private List<Address>
_addresses;
//Method for update Address and duplicate checks
public string UpdateAddress(Address personAddress)
{
string requestCorrelationId = _client.GetCorrelationRequest();
var userContext = _sessionService.Get<UserContext>(SessionKeys.UserContext);
try
{
if (IsDuplicateAddress(personAddress))
{
{
string requestCorrelationId = _client.GetCorrelationRequest();
var userContext = _sessionService.Get<UserContext>(SessionKeys.UserContext);
try
{
if (IsDuplicateAddress(personAddress))
{
return "Duplicate some
Message"
}
}
}
//Private Method
private bool IsDuplicateAddress(Address personAddress)
{
return _addresses().Any(p => p.Equals(personAddress));
}
{
return _addresses().Any(p => p.Equals(personAddress));
}
//Implement IEquatable
public partial class Address : IEquatable<Address>
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string Suburb { get; set; }
public string State { get; set; }
public string PostCode { get; set; }
public string CountryCode { get; set; }
public long? Id { get; set; }
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string AddressLine3 { get; set; }
public string Suburb { get; set; }
public string State { get; set; }
public string PostCode { get; set; }
public string CountryCode { get; set; }
public long? Id { get; set; }
public bool Equals(Address newAddress)
{
if (newAddress == null) return false;
return
{
if (newAddress == null) return false;
return
string.Equals(Regex.Replace(newAddress.AddressLine1 ??
string.Empty, @"\s", string.Empty),
Regex.Replace(AddressLine1, @"\s", string.Empty),
StringComparison.OrdinalIgnoreCase)
&& string.Equals(Regex.Replace(newAddress.AddressLine2 ??
&& string.Equals(Regex.Replace(newAddress.AddressLine2 ??
string.Empty, @"\s", string.Empty),
Regex.Replace(AddressLine2, @"\s", string.Empty),
StringComparison.OrdinalIgnoreCase)
&& string.Equals(Regex.Replace(newAddress.AddressLine3 ??
&& string.Equals(Regex.Replace(newAddress.AddressLine3 ??
string.Empty, @"\s", string.Empty),
Regex.Replace(AddressLine3, @"\s", string.Empty),
StringComparison.OrdinalIgnoreCase)
&& string.Equals(newAddress.CountryCode ??
&& string.Equals(newAddress.CountryCode ??
string.Empty,CountryCode,
StringComparison.OrdinalIgnoreCase)
&& string.Equals(newAddress.PostCode ??
&& string.Equals(newAddress.PostCode ??
string.Empty,PostCode,
StringComparison.OrdinalIgnoreCase)
&& string.Equals(newAddress.State ??
&& string.Equals(newAddress.State ??
string.Empty, State,
StringComparison.OrdinalIgnoreCase)
&& string.Equals(Regex.Replace(newAddress.Suburb ??
&& string.Equals(Regex.Replace(newAddress.Suburb ??
string.Empty, @"\s", string.Empty),
Regex.Replace(Suburb, @"\s", string.Empty),
StringComparison.OrdinalIgnoreCase);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals(obj as Address);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = AddressLine1?.GetHashCode() ?? 0;
hashCode = (hashCode*397) ^ (AddressLine2?.GetHashCode() ?? 0);
hashCode = (hashCode*397) ^ (AddressLine3?.GetHashCode() ?? 0);
hashCode = (hashCode*397) ^ (Suburb?.GetHashCode() ?? 0);
hashCode = (hashCode*397) ^ (State?.GetHashCode() ?? 0);
hashCode = (hashCode*397) ^ (PostCode?.GetHashCode() ?? 0);
hashCode = (hashCode*397) ^ (CountryCode?.GetHashCode() ?? 0);
hashCode = (hashCode*397) ^ Id.GetHashCode();
return hashCode;
}
}
}
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals(obj as Address);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = AddressLine1?.GetHashCode() ?? 0;
hashCode = (hashCode*397) ^ (AddressLine2?.GetHashCode() ?? 0);
hashCode = (hashCode*397) ^ (AddressLine3?.GetHashCode() ?? 0);
hashCode = (hashCode*397) ^ (Suburb?.GetHashCode() ?? 0);
hashCode = (hashCode*397) ^ (State?.GetHashCode() ?? 0);
hashCode = (hashCode*397) ^ (PostCode?.GetHashCode() ?? 0);
hashCode = (hashCode*397) ^ (CountryCode?.GetHashCode() ?? 0);
hashCode = (hashCode*397) ^ Id.GetHashCode();
return hashCode;
}
}
}
No comments :
Post a Comment