0

I have the following class and I've two object of it,Obj1 one have the previous data and obj2 have the data with some fields that can be changed (Im in action of edit which have obj1 before changing and obj2 after).my question is if I have the two object how is the best way to put in object (like list of key val) just the fields that was changed and they value. I read about it in the SO and I found this two approach but some of the post are old..., what is right/efficient way to go ?example will be very helpful.

Comparing objects

Get the changed properties in same object

  public class UserData
    {
        public int Id { get; set; }
        public string UserName { get; set; }
        public string Address { get; set; }
        public string Email { get; set; }
        public string Work { get; set; }
        public string Home { get; set; }
    }

public class Program
{   
   public static void Main()
   {


    UserData obj1 = new UserData();

    obj1.Email = "www.test.com";

    obj1.Home = "test home";
    UserData obj2 = new UserData();

    obj2.Email = "www.test2.com";
    obj2.Home = "test home2";

        }

 }

I've tried like the following from this post but I got error,any idea? Compare two objects and find the differences

changedList = obj1.DetailedCompare(obj2);

I got this error,any idea how to solve it:

The type arguments for method 'Web.Controllers.extentions.DetailedCompare<T>(T, T)' cannot be inferred from the usage. Try specifying the type arguments explicitly

Community
  • 1
  • 1
07_05_GuyT
  • 2,787
  • 14
  • 43
  • 88
  • @PatrickHofman-not sure that I got it... – 07_05_GuyT Jul 06 '14 at 08:02
  • 1
    What does `obj1-email = "www.test.com";` mean? – Konrad Kokosa Jul 06 '14 at 08:03
  • @KonradKokosa-this is just example for the property of the object that was changed from the previos...fix my post and changed the second to obj2... – 07_05_GuyT Jul 06 '14 at 08:07
  • @PatrickHofman- what do you mean by doesn't compile ?In my example there is 6 properties that just two of them(home,email) where changed and I want to get them ,how?I try with the example in the bottom without succees any idea? – 07_05_GuyT Jul 06 '14 at 08:24
  • Means that the way you are accessing `properties` is not syntactically correct. The correct way is to use a `.` instead of `-`. Like: `obj2.email` not `obj2-email` – Shaharyar Jul 06 '14 at 08:26
  • @PatrickHofman-Thanks Patrick I've fix it in the post,miss the capital :( ,is it ok now? – 07_05_GuyT Jul 06 '14 at 08:36
  • If your extension method really has the signature `DetailedCompare(this T val1, T val2)` like in the linked thread, and if `obj1` and `obj2` both have compile-time type `UserData` like above, then the call `obj1.DetailedCompare(obj2)` does **not** give the error we see in your question (unable to infer type arguments). Give a complete self-contained example that reproduces the problem, or else we cannot be helpful. – Jeppe Stig Nielsen Jul 06 '14 at 08:43
  • [Check this answer for comparing objects](http://stackoverflow.com/a/37345928/5985860) – Akshay Jun 15 '16 at 13:13

1 Answers1

2

You have to create function which list and compare all properties of the two objects. This can be done by reflection :

public class UserData
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
    public string Work { get; set; }
    public string Home { get; set; }

    public IEnumerable<PropertyInfo> GetVariance(UserData user)
    {
        foreach (PropertyInfo pi in user.GetType().GetProperties()) {

            object valueUser = typeof(UserData).GetProperty (pi.Name).GetValue (user);
            object valueThis = typeof(UserData).GetProperty (pi.Name).GetValue (this);

            if (valueUser != null && !valueUser.Equals(valueThis))
                yield return pi;

        }
    }

}

I use the "Equals method" to compare the values of strings, int, etc and not their references contrary to an "==" (which compare reference here, because we get an object type).

        UserData obj1 = new UserData();

        obj1.Email = "www.test.com";
        obj1.Home = "test home";
        UserData obj2 = new UserData();

        obj2.Email = "www.test2.com";
        obj2.Home = "test home2";

        IEnumerable<PropertyInfo> variances = obj1.GetVariance (obj2);

        foreach (PropertyInfo pi in variances)
            Console.WriteLine (pi.Name);

Caution, it work only with primitive's types (int, string, float, ...) because Equals compare the references of two object.

csblo
  • 1,571
  • 13
  • 20
  • Thanks a lot Voted up!,one last question all my properties are strings beside two properties which are inside another object,how should I get them with reflecation? – 07_05_GuyT Jul 06 '14 at 09:05
  • If these propertie's type is a class that you had defined, you can override "Equals" or look at this link [Compare two complex object](http://stackoverflow.com/questions/10454519/best-way-to-compare-two-complex-object), else it's more complicated... and unfortunatly I don't know what is the best method. – csblo Jul 06 '14 at 10:20