So I have a table that I track changes on with cdc in sql I have a requirement to find what specific values in my object have changed in a given time. When I get my history records its basically a list of all the same object from its point of time when it changed. I need an efficient way to use this list to identify exactly what fields changed within the object
using linq I am doing this but I am just curious is there a better way?
Value01Changed = auditHistory.Any(x =>
x.AuditAction == "Insert" ||
auditHistory.Any(y => y.Value01 != x.Value01)),
Value02Changed = auditHistory.Any(x =>
x.AuditAction == "Insert" ||
auditHistory.Any(y => y.Value02 != x.Value02)),
Value03Changed = auditHistory.Any(x =>
x.AuditAction == "Insert" ||
auditHistory.Any(y => y.Value03 != x.Value03)),
For clarity this solution works I just think it is expensive and in-efficient and am curious if there is a better way
Based on Peter's and John Wu's answer i implemented the following in an extension method which has tidied up my code and improved performance
var hasInserts = auditHistory.Any(x => x.AuditAction == "Insert");
Value01Changed = auditHistory.HasPropertyChanges(x => x.Value01 , hasInserts);
Value02Changed = auditHistory.HasPropertyChanges(x => x.Value02, hasInserts);
public static class HasPropertyChanged
{
public static bool HasPropertyChanges(this IEnumerable<AuditRecord> list, Func<AuditRecord, object> func,bool hasInserts)
{
if(hasInserts) return true;
return list.GroupBy(func).Count() > 1;
}
}