比较两个阵列

时间:2011-08-19 10:19:58

标签: c#

我正在尝试比较2个对象数组,以检查它们之间的值是否发生了变化。 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" } };

数组中的每个项目代表下面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; }
}

我需要创建一个比较2个对象的方法。 e.g。

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

复杂的部分是它只比较没有应用IgnoreAudit属性的简单属性,只比较应用了ChildAudit属性的复杂属性。

因此,从上面的示例中,将对Profile属性进行递归比较(因为它应用了ChildAudit属性),而Role属性则不会。另外说如果NumViews是旧状态和新状态之间唯一值的变化,IsDirty将返回false(因为它应用了IgnoreAudit属性)。

为了进一步帮助你,鉴于上面的旧状态,这里有一些新状态的示例,以及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" } };

我希望我的意图明确。如果您需要任何其他信息,请随时发表评论。

欣赏帮助。感谢

1 个答案:

答案 0 :(得分:1)

您可以使用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
    }

}