简体   繁体   English

比较两个数组

[英]Compare Two Arrays

I'm trying to compare 2 object arrays to check whether the values have changed between them. 我正在尝试比较2个对象数组,以检查它们之间的值是否已更改。 eg 例如

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): 数组中的每一项代表下面User类中一个属性的值(按它们定义的顺序):

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. 我需要创建一种方法来比较这两个对象。 eg 例如

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. 复杂的部分是,它仅比较未应用IgnoreAudit属性的简单属性,而仅比较已应用ChildAudit属性的复杂属性。

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. 因此,从上面的示例中,将对Profile属性进行递归比较(因为它应用了ChildAudit属性),而Role属性则没有。 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). 还要说一下,如果NumViews是唯一在新旧状态之间变化的值,则IsDirty将返回false(因为已应用IgnoreAudit属性)。

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: 为了进一步帮助您,给定上面的旧状态,这里有一些新状态的示例,以及IsDirty是返回true还是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 谢谢

You can use the IComparable Interface to determine if two classes are equal. 您可以使用IComparable接口确定两个类是否相等。

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
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM