c# - 比较两个数组

标签 c#

我正在尝试比较 2 个对象数组以检查它们之间的值是否发生了变化。例如

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; }
}

我需要创建一个方法来比较这两个对象。例如

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" } };

我希望我已经表达清楚了我的意图。如果您需要任何其他信息,请随时发表评论。

感谢帮助。谢谢

最佳答案

您可以使用 IComparable 接口(interface)来确定两个类是否相等。

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
    }

}

关于c# - 比较两个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7120174/

相关文章:

c# - 当内容较长时,itextsharp 不会创建新页面

c# - LINQ to Entities 无法识别方法组连接

c# - “如果”语句

C# 检查 MaskedTestBox 是否为空

c# - 将 XDocument 转换为流

c# - VB 到 C# 属性到 int 转换的转换

c# - "connection has been disabled"快速创建/删除数据库时的错误信息

c# - ASP.NET MVC - 使用 Automapper 进行映射

C# 在没有 Excel 的情况下将 .xls 转换为 .csv

c# - 微软 Surface vs Windows Touch?