获取具有相同属性的两个对象之间的差异 [英] Getting Difference Between Two Objects With Same Properties

查看:58
本文介绍了获取具有相同属性的两个对象之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取使用EmployeeHistory模型(对象2)对Employee模型(对象1)进行的更改的列表.基本上,只有一个雇员记录,但是有多个EmployeeHistory记录.每次对Employee进行更改时,都会将新记录添加到EmployeeHistory表中,该表包含更改前Employee的数据.我想要一种比较每个EmployeeHistory记录并返回报告所做更改的字符串列表的方法.因此,为了获取更改列表,我想遍历EmployeeHistory记录的列表,并将每个EmployeeHistory记录与以前的EmployeeHistory记录进行比较.最后的EmployeeHistory记录将需要与属性非常相似的当前Employee(对象1)记录进行比较.有什么方法可以做到,而又没有大量的IF语句比较每条记录的两个属性?

I am trying get a list of the changes that were made to an Employee model (object 1) using an EmployeeHistory model (object 2). Basically, there is one single employee record but there are several EmployeeHistory records. Every time something is changed on an Employee a new record is added to the EmployeeHistory table which contains the data of the Employee prior to the change. I want a way to compare each of the EmployeeHistory Records and return a list of strings reporting the changes that were made. So in order to get the list of changes I want to iterate through the list of EmployeeHistory records and compare each EmployeeHistory Record with the previous EmployeeHistory record. And the very last EmployeeHistory record will need to be compared to the current Employee (object 1) record which is very similar in properties. Is there any way to do this without having an insane amount of IF statements comparing the two properties on each of the records?

这就是我要寻找的东西

 public List<string> GetEmployeeMasterHistory(Models.EmployeeMaster employee,IEnumerable<EmployeeMasterHistory> employeeHistoryCollection)
 {
       foreach (var historyRecord in employeeHistoryCollection)
       {
          //Compare historyRecord to EmployeeCollection[historyRecord.Index() - 1]
       }
       return null;
 }

我已经有一个方法可以对每个属性进行所有检查,但是将来会添加更多的属性,而且我厌倦了必须添加新的IF语句,而且看起来不太

I already have a method that does all the check for each of the properties but there is going to be a lot more properties added in the future and I tired of having to add the new IF statements and it doesn't seem very efficient.

这是EmployeeMasterHistory记录的样子:

Here is what a EmployeeMasterHistory Record looks like:

 public partial class EmployeeMasterHistory
    {
        public Nullable<int> EmployeeNumber { get; set; }
        public Nullable<int> CompanyNumber { get; set; }
        public string UserName { get; set; }
        public string Initials { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName { get; set; }
        public Nullable<bool> StatusFlag { get; set; }
        public Nullable<System.DateTime> StartDate { get; set; }
        public Nullable<System.DateTime> TerminationDate { get; set; }
        public string Branch { get; set; }
        public Nullable<int> DepartmentNumber { get; set; }
        public string Supervisor { get; set; }
        public Nullable<int> Shift { get; set; }
        public Nullable<int> UnionNo { get; set; }
        public string G2ID { get; set; }
        public Nullable<bool> EnterTimeFl { get; set; }
        public string Phone { get; set; }
        public string Extension { get; set; }
        public string CellPhone { get; set; }
        public string Email { get; set; }
        public Nullable<int> PrimaryJobRole { get; set; }
        public Nullable<int> JobLevel { get; set; }
        public Nullable<int> JobGroup { get; set; }
        public string JobTitle { get; set; }
        public string EmployeeType { get; set; }
        public string PayType { get; set; }
        public Nullable<decimal> Rate { get; set; }
        public Nullable<System.DateTime> LastReviewDate { get; set; }
        public Nullable<System.DateTime> NextReviewDate { get; set; }
        public Nullable<System.DateTime> LastPayChangeDate { get; set; }
        public string EmergencyContact { get; set; }
        public string EmergencyContactRelationship { get; set; }
        public string EmergencyContactPhone { get; set; }
        public Nullable<bool> CPComputer { get; set; }
        public Nullable<bool> CPPhone { get; set; }
        public Nullable<bool> CPCreditCard { get; set; }
        public Nullable<bool> CPGasCard { get; set; }
        public Nullable<bool> CPKeys { get; set; }
        public Nullable<bool> CPSecurityCard { get; set; }
        public Nullable<bool> CPVehicle { get; set; }
        public Nullable<bool> CPTools { get; set; }
        public Nullable<bool> CPUniform { get; set; }
        public string ModBy { get; set; }
        public Nullable<System.DateTime> ModDate { get; set; }
        public int ID { get; set; }
        public string SalesRep { get; set; }
        public string MiddleName { get; set; }
        public Nullable<int> ManagerEmpNo { get; set; }
        public Nullable<bool> TempFl { get; set; }
        public Nullable<bool> PEWFl { get; set; }
        public Nullable<bool> PGTFl { get; set; }
        public Nullable<bool> PMPFl { get; set; }
        public Nullable<bool> PPGEFl { get; set; }
        public Nullable<bool> PPGFl { get; set; }
        public Nullable<bool> PRCFl { get; set; }
        public Nullable<bool> PTCFl { get; set; }
        public Nullable<bool> PPFl { get; set; }
        public Nullable<bool> SWPFl { get; set; }
        public Nullable<int> PrimaryDivision { get; set; }
        public string TechGroupID { get; set; }
        public string TechLevelID { get; set; }
        public Nullable<bool> TechATD { get; set; }
        public Nullable<int> ReviewPeriod { get; set; }
        public Nullable<bool> CorpFl { get; set; }
    }

提前谢谢!

推荐答案

以下是使用反射的非常简单的方法:

Here is a very simple approach using reflection:

        var oOldRecord = new EmployeeMasterHistory();
        oOldRecord.EmployeeNumber = 1;
        var oNewRecord = new EmployeeMasterHistory();
        oNewRecord.EmployeeNumber = 2;
        oNewRecord.CompanyNumber = 3;

        var oType = oOldRecord.GetType();

        foreach (var oProperty in oType.GetProperties())
        {
            var oOldValue = oProperty.GetValue(oOldRecord, null);
            var oNewValue = oProperty.GetValue(oNewRecord, null);
            // this will handle the scenario where either value is null
            if (!object.Equals(oOldValue, oNewValue))
            {
                // Handle the display values when the underlying value is null
                var sOldValue = oOldValue == null ? "null" : oOldValue.ToString();
                var sNewValue = oNewValue == null ? "null" : oNewValue.ToString();

                System.Diagnostics.Debug.WriteLine("Property " + oProperty.Name + " was: " + sOldValue + "; is: " + sNewValue);
            }
        }

此示例的输出是:

Property EmployeeNumber was: 1; is: 2
Property CompanyNumber was: null; is: 3

这可能需要清理,但应该可以帮助您正确地开始工作.

This probably needs cleanup, but should get you started down the right path.

这篇关于获取具有相同属性的两个对象之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆