【发布时间】:2020-06-01 13:35:46
【问题描述】:
我有两个 Profile 类的对象,我想比较它以获得差异。为了更好地解释:
public partial class Profile
{
public long Id { get; set; }
public long VisitorId { get; set; }
public string Description { get; set; }
public long Age { get; set; }
public DateTime? LastUpdate { get; set;}
}
我想在我的方法中了解对象oldProfile 和newProfile 之间的区别以制作变更日志。
例如,如果oldProfile 有Age = 10 和Description = "old" 和newProfile 有Age = 11 和Description = "new",我现在将这个差异在我的数据库中进行两个不同的插入:
public void PostChangelogProfileDetail(Profile oldProfile, Profile newProfile)
{
ProfileDetailChangeLog profileDetailChangeLog = new ProfileDetailChangeLog();
//COMPARE oldProfile AND newProfile
foreach ( //difference resulted in the compare)
{
profileDetailChangeLog.VisitorId = newProfile.VisitorId;
profileDetailChangeLog.ModifiedRecord = //name of the attribute modified (Age, Description, etc...)
_profileDetailChangeLog.Create(profileDetailChangeLog);
}
}
【问题讨论】:
-
比较对象有多种方法 1) 编写代码以逐个属性比较属性 2) 使用库;例如,您可以使用 Json 来查找使用 jsondiffpatch.net 的差异。检查这个答案 - stackoverflow.com/questions/24876082/…
标签: c# asp.net-core entity-framework-core compare