Próbuję uzyskać listę zmian, które zostały wprowadzone do modelu pracownika (obiekt 1) przy użyciu modelu EmployeeHistory (obiekt 2). Zasadniczo istnieje jeden rekord pracownika, ale istnieje kilka rekordów EmployeeHistory. Za każdym razem, gdy coś zmieni się w Pracowniku, nowy rekord jest dodawany do tabeli EmployeeHistory, która zawiera dane Pracownika przed zmianą. Chcę porównać każdą z rekordów EmployeeHistory i zwrócić listę ciągów informujących o wprowadzonych zmianach. Aby uzyskać listę zmian, chcę przetestować listę rekordów EmployeeHistory i porównać każdy rekord EmployeeHistory z poprzednim rekordem EmployeeHistory. I ostatni rekord EmployeeHistory będzie musiał być porównany z bieżącym rekordem Employee (obiekt 1), który jest bardzo podobny do właściwości. Czy jest jakiś sposób, aby to zrobić bez szalonej ilości instrukcji IF porównujących dwie właściwości w każdym z rekordów?Uzyskiwanie różnicy między dwoma obiektami o tych samych właściwościach
Jest to rodzaj co szukam:
public List<string> GetEmployeeMasterHistory(Models.EmployeeMaster employee,IEnumerable<EmployeeMasterHistory> employeeHistoryCollection)
{
foreach (var historyRecord in employeeHistoryCollection)
{
//Compare historyRecord to EmployeeCollection[historyRecord.Index() - 1]
}
return null;
}
już mam metodę, która wykonuje całą kontrolę dla każdej właściwości, ale nie będzie o wiele więcej właściwości dodane w przyszłości i zmęczony koniecznością dodawania nowych instrukcji IF i nie wydaje się bardzo wydajny.
Oto co EmployeeMasterHistory Record wygląda następująco:
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; }
}
Z góry dziękuję!
Brzmi jak zadanie do odbicia od czubka głowy. –
Możesz użyć odbicia, aby zidentyfikować właściwości do porównania. Wtedy jest to tylko kwestia użycia pętli zamiast pisania nowej klauzuli if dla każdej właściwości. Jeśli chcesz porównać tylko niektóre właściwości, możesz zdefiniować niestandardowy atrybut, aby włączyć właściwości do porównania lub zrezygnować. –
Zrobiłem trochę badań nad refleksją, ponieważ jestem dla niej nowy. Zamierzam spróbować stworzyć rozwiązanie wykorzystujące to. Dzięki chłopaki, to bardzo pomocne. – user3788671