1

At the moment I currently have a CompanyModel that looks like so;

public class CompanyModel
{
    public string CompanyID { get; set; }
    public string CompanyName { get; set; }
    public string CompanyAddr1 { get; set; }
    public string CompanyAddr2 { get; set; }
    public string CompanyTown { get; set; }
    public string CompanyCounty { get; set; }
    public string CompanyPcode { get; set; }
    public string CompanyTelephone { get; set; }
    public string CompanyAltTelephone { get; set; }
    public string CompanyFax { get; set; }
    public string CompanyEmail { get; set; }
    public string CompanyWhoEntered { get; set; }
    public DateTime CompanyDateEntered { get; set; }
    public string CompanyTimeEntered { get; set; }
    public string CompanyGeographicArea { get; set; }
    public string CompanySearchName { get; set; }

}

What I would like to do is initialise two CompanyModels and compare the contents of both to ensure that both Companies have exactly the same data in their fields. Currently I am doing an absolutely horrendous concatenation of If statements as I am unsure of another way. This currently looks like so;

if (eCDetails.CompanyName == cCompanyDetails.CompanyName)
   {
      if (eCDetails.CompanyName == cCompanyDetails.CompanyName)
      {
          if (eCDetails.CompanyAddr1 == cCompanyDetails.CompanyName)
          {

and so on and so forth (it's terrible). Is there an easier to way to ensure that both CompanyModels are equivalent?

CBreeze
  • 2,925
  • 4
  • 38
  • 93
  • You could at least stop the nesting by reversing the logic: `if(x.prop != y.prop) return false;` and `return true;` at the end. – DavidG Nov 25 '15 at 13:47
  • 4
    See [this post](http://stackoverflow.com/questions/10454519/best-way-to-compare-two-complex-object) – Yuval Itzchakov Nov 25 '15 at 13:48

4 Answers4

4

How about using the conditional-AND (&&) operator?

if (eCDetails.CompanyName == cCompanyDetails.CompanyName &&
    eCDetails.CompanyName == cCompanyDetails.CompanyName &&
    eCDetails.CompanyAddr1 == cCompanyDetails.CompanyName &&
    // etc...

If you have ReSharper, you can use it to auto-generate this code (and more) for you.

Community
  • 1
  • 1
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
1

You are trying to write an equals method ?

You can make something like that :

if (eCDetails.CompanyName != cCompanyDetails.CompanyName)
   { return false;
}
if (eCDetails.CompanyName != cCompanyDetails.CompanyName)
      {return false;
}
...
return true;

There's no many option to do what you want :)

SebVb
  • 187
  • 1
  • 3
  • 14
1

try this .. also you have to ignore some properties

public static bool PublicInstancePropertiesEqual<T>(this T self, T to, params string[] ignore) where T : class
{
    if (self != null && to != null)
    {
        var type = typeof(T);
        var ignoreList = new List<string>(ignore);
        var unequalProperties =
            from pi in type.GetProperties(BindingFlags.Public | BindingFlags.Instance)
            where !ignoreList.Contains(pi.Name)
            let selfValue = type.GetProperty(pi.Name).GetValue(self, null)
            let toValue = type.GetProperty(pi.Name).GetValue(to, null)
            where selfValue != toValue && (selfValue == null || !selfValue.Equals(toValue))
            select selfValue;
        return !unequalProperties.Any();
    }
    return self == to;
}

Orginal thread

Happy coding

Community
  • 1
  • 1
Jagadeesh Govindaraj
  • 6,977
  • 6
  • 32
  • 52
1

You may also implement Equals ony youd model-class:

class CompanyModel{
    public override bool Equals(object other) {
        var m = (CompanyModel) other;
        if (m == null) return false;

        return this.CompanyName == m.CompanyName &&
            this.CompanyName == m.CompanyName &&
            this.CompanyAddr1 == m.CompanyAddr1 // ...
    }
}

Now you can use myModelInstance.Equals(anotherInstance).

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111