nfplee
nfplee

Reputation: 7987

Compare Two Arrays

I'm trying to compare 2 object arrays to check whether the values have changed between them. e.g.

var oldState = new object[] { 1, "Lee", 0, new UserProfile { FirstName = "Lee",
    LastName = "Timmins" }, new Role { RoleID = 1, RoleName  "User" } };

var newState = new object[] { 1, "Lee", 1, new UserProfile { FirstName = "Lee",
    LastName = "Timmins" }, new Role { RoleID = 1, RoleName = "User" } };

Each item in the array represents a value for a property from the User class below (in the order they are defined):

public class User {
   public int UserID { get; set; }
   public string UserName { get; set; }

   [IgnoreAudit]
   public int NumViews { get; set; }

   [ChildAudit]
   public UserProfile Profile { get; set; }

   public Role Role { get; set; }
}

public class UserProfile {
   public string FirstName { get; set; }
   public string LastName { get; set; }
}

public class Role {
   public int RoleID { get; set; }
   public string RoleName { get; set; }
}

I need to create a method to compare the 2 objects. e.g.

public bool IsDirty(object[] oldState, object[] newState) {
   // Comparision code here
}

The complicated part is that it only compares the simple properties that don't have the IgnoreAudit attribute applied to them and only compares complex properties that have ChildAudit attribute applied.

Therefore from the example above the Profile property would be recursively compared (since it has the ChildAudit attribute applied) whereas the Role property wouldn't. Also say if NumViews is the only value that changes between the old and new state IsDirty would return false (since it has the IgnoreAudit attribute applied).

To help you further, given the old state above here is a few examples of the new state and whether IsDirty would return true or false:

// False - since only NumViews changed
var newState1 = new object[] { 1, "Lee", 1, new UserProfile { FirstName = "Lee",
    LastName = "Timmins" }, new Role { RoleID = 1, RoleName = "User" } };

// True - since Username has changed
var newState2 = new object[] { 1, "Lee2", 1, new UserProfile { FirstName = "Lee",
    LastName = "Timmins" }, new Role { RoleID = 1, RoleName = "User" } };

// True - since Profile.FirstName has changed
var newState3 = new object[] { 1, "Lee", 1, new UserProfile { FirstName = "Paul",
    LastName = "Timmins" }, new Role { RoleID = 1, RoleName = "User" } };

// False - since only Role.RoleName has changed
var newState4 = new object[] { 1, "Lee", 1, new UserProfile { FirstName = "Lee",
    LastName = "Timmins" }, new Role { RoleID = 1, RoleName = "Admin" } };

I hope i've made my intentions clear. Please feel free to comment if there's any additional information you require.

Appreciate the helps. Thanks

Upvotes: 2

Views: 257

Answers (1)

Jethro
Jethro

Reputation: 5916

You can use the IComparable Interface to determine if two classes are equal.

class Program
{
    static void Main(string[] args)
    {
        var newState1 = new User
                        {
                            UserId = 1,
                            UserName = "Lee",
                            NumViews = 1,
                            Profile = new UserProfile
                                      {
                                          FirstName = "Lee",
                                          LastName = "Timmins"
                                      },
                            RoleMember = new Role {RoleId = 1, RoleName = "User"}
                        };


        // True - since Username has changed
        var newState2 = new User
                        {
                            UserId = 1,
                            UserName = "Lee2",
                            NumViews = 1,
                            Profile = new UserProfile
                                      {
                                          FirstName = "Lee",
                                          LastName = "Timmins"
                                      },
                            RoleMember = new Role {RoleId = 1, RoleName = "User"}
                        };
        //Will show 1 or -1 if not state has change. If == 0 then state has not changed.
        Console.WriteLine("Has State1 Changed? : {0}", newState1.CompareTo(newState2));
        Console.ReadLine();

    }

    public class User : IComparable<User>
    {
        public int UserId { get; set; }
        public string UserName { get; set; }
        public int NumViews { get; set; }
        public UserProfile Profile { get; set; }
        public Role RoleMember { get; set; }

        #region Implementation of IComparable<in User>

        public int CompareTo(User other)
        {
            if (UserId.CompareTo(other.UserId) != 0)
            {
                return UserId.CompareTo(other.UserId);
            }
            if (UserName.CompareTo(other.UserName) != 0)
            {
                return UserName.CompareTo(other.UserName);
            }
            if (NumViews.CompareTo(other.NumViews) != 0)
            {
                return NumViews.CompareTo(other.NumViews);
            }
            if (Profile.CompareTo(other.Profile) != 0)
            {
                return Profile.CompareTo(other.Profile);
            }
            return RoleMember.CompareTo(other.RoleMember) != 0 ? RoleMember.CompareTo(other.RoleMember) : 0;
        }

        #endregion
    }

    public class UserProfile : IComparable<UserProfile>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        #region Implementation of IComparable<in UserProfile>

        public int CompareTo(UserProfile other)
        {
            return FirstName.CompareTo(other.FirstName) == 0 ? LastName.CompareTo(other.LastName) : FirstName.CompareTo(other.FirstName);
        }

        #endregion
    }

    public class Role : IComparable<Role>
    {
        public int RoleId { get; set; }
        public string RoleName { get; set; }

        #region Implementation of IComparable<in Role>

        public int CompareTo(Role other)
        {
            return RoleId.CompareTo(other.RoleId) == 0 ? RoleName.CompareTo(other.RoleName) : RoleId.CompareTo(other.RoleId);
        }

        #endregion
    }

}

Upvotes: 1

Related Questions