Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a two models (Current and Original) that have the same properties. I want to build in a validation to check if one of the properties (Address - which is a collection consisting of "Country, City, Suburb, Code") have the same values or they have been updated.

below code I know it works when comparing normal string but I am comparing Collectin/List.

What I have tried:

if(current.Student.Address == Original.Address){
//show no updates
}
else{
//Update
}
Posted
Updated 19-Jul-23 4:30am
v3
Comments
PIEBALDconsult 19-Jul-23 8:52am    
The question isn't clear.
Can you add a comparison method to the class?
FuFu Dev's 19-Jul-23 9:02am    
I have rephrase the question for better understanding. thanks.
PIEBALDconsult 19-Jul-23 9:06am    
Yeah, OK, so if the Address is a class, can you add a comparison method to it?
FuFu Dev's 19-Jul-23 9:11am    
I am not sure if I am understanding you. but the scenario is that in hand I have Address 1 and Address 2 which have the same properties. I want a simple check that indeed those two addresses have got the same values.
PIEBALDconsult 20-Jul-23 10:41am    
So does the Address class simply contain a list (collection) of strings?

1 solution

If you have access to the code for the Address class, you can add the ability for them to compare themselves.
The following is a very naïve example. Comparing addresses is a very complex topic with many pitfalls.

C#
public partial class Address : System.IEquatable<Address>
{
  private static System.StringComparer comparer
    = System.StringComparer.InvariantCultureIgnoreCase ;

  public string Street1      { get ; private set ; }
  public string Street2      { get ; private set ; }
  public string CityStateZip { get ; private set ; }

  private int hashcode ;

  public Address
  (
    string Street1
  ,
    string Street2
  ,
    string CityStateZip
  )
  {
    this.Street1      = Street1      ?? System.String.Empty ;
    this.Street2      = Street2      ?? System.String.Empty ;
    this.CityStateZip = CityStateZip ?? System.String.Empty ;

    this.hashcode = ( this.Street1 + this.Street2 + this.CityStateZip ).GetHashCode() ;

    return ;
  }

  public override int
  GetHashCode
  (
  )
  {
    return ( this.hashcode ) ;
  }

  public override bool
  Equals
  (
    object Op1
  )
  {
    return ( this.Equals ( Op1 as Address ) ) ;
  }

  public virtual bool
  Equals
  (
    Address Op1
  )
  {
    return
    (
      ( Op1 != null )
    &&
      ( comparer.Compare ( this.Street1      , Op1.Street1      ) == 0 )
    &&
      ( comparer.Compare ( this.Street2      , Op1.Street2      ) == 0 )
    &&
      ( comparer.Compare ( this.CityStateZip , Op1.CityStateZip ) == 0 )
    ) ;
  }

  public static bool
  operator ==
  (
    Address Op0
  ,
    Address Op1
  )
  {
    return ( Op0.Equals ( Op1 ) ) ;
  }

  public static bool
  operator !=
  (
    Address Op0
  ,
    Address Op1
  )
  {
    return ( !Op0.Equals ( Op1 ) ) ;
  }
}
 
Share this answer
 
Comments
BillWoodruff 20-Jul-23 3:29am    
+5
FuFu Dev's 20-Jul-23 6:08am    
This would be good solution if only I had access to the Address class but unfortunately I don’t have access all I have is what it has returned. But thanks for the answer much appreciated 🫶🏽
PIEBALDconsult 20-Jul-23 9:00am    
Ah, OK, I thought you might not. Your best choice may be to write an Extension Method to do it.
FuFu Dev's 20-Jul-23 9:16am    
Yes that's what I was thinking and trying to avoid another method. I know with a normal string you can just compare it with a simple code like: if(Model1.name == Model2.Name){//Do Something} and the with CodeDescription you can do something like: if(Model1.StudentType.Code == Model2.StudentType.code && Model1.StudentType.Description == Model2.StudentType.Description){//Do Something}........... but now I have no Idea how to do it for a collection or List :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900