before.Except(after)
will depend on the
Bills
class correctly implementing the
IEquatable<Bills>
interface, or at the very least providing correct overloads of
GetHashCode
and
Equals(object)
.
before.Intersect(after, new BillsComparer())
will only return pairs which the comparer thinks are equal. That's no good for detecting changes.
The reflection code will be very slow, and not really necessary since you know the types at compile-time, and you don't have to worry about derived types.
Perhaps try something like this:
public readonly struct PropertyDifference
{
public PropertyDifference(string propertyName, object oldValue, object newValue)
{
PropertyName = propertyName;
OldValue = oldValue;
NewValue = newValue;
}
public string PropertyName { get; }
public object OldValue { get; }
public object NewValue { get; }
}
public sealed class BillsDifference
{
private BillsDifference(int id, List<PropertyDifference> changedProperties)
{
Id = id;
ChangedProperties = changedProperties;
}
public int Id { get; }
public List<PropertyDifference> ChangedProperties { get; }
private static BillsDifference Create(Bills oldValue, Bills newValue)
{
int id;
List<PropertyDifference> changedProperties = new List<PropertyDifference>(7);
if (oldValue != null && newValue != null)
{
id = oldValue.Id;
if (!Equals(oldValue.Party, newValue.Party))
{
changedProperties.Add(new PropertyDifference(nameof(Bills.Party), oldValue.Party, newValue.Party));
}
if (!Equals(oldValue.BillNo, newValue.BillNo))
{
changedProperties.Add(new PropertyDifference(nameof(Bills.BillNo), oldValue.BillNo, newValue.BillNo));
}
if (!Equals(oldValue.BillDt, newValue.BillDt))
{
changedProperties.Add(new PropertyDifference(nameof(Bills.BillDt), oldValue.BillDt, newValue.BillDt));
}
if (!Equals(oldValue.Amt, newValue.Amt))
{
changedProperties.Add(new PropertyDifference(nameof(Bills.Amt), oldValue.Amt, newValue.Amt));
}
if (!Equals(oldValue.DueDt, newValue.DueDt))
{
changedProperties.Add(new PropertyDifference(nameof(Bills.DueDt), oldValue.DueDt, newValue.DueDt));
}
if (!Equals(oldValue.PaidOn, newValue.PaidOn))
{
changedProperties.Add(new PropertyDifference(nameof(Bills.PaidOn), oldValue.PaidOn, newValue.PaidOn));
}
if (!Equals(oldValue.Remarks, newValue.Remarks))
{
changedProperties.Add(new PropertyDifference(nameof(Bills.Remarks), oldValue.Remarks, newValue.Remarks));
}
}
else if (oldValue != null)
{
id = oldValue.Id;
changedProperties.Add(new PropertyDifference(nameof(Bills.Party), oldValue.Party, null));
changedProperties.Add(new PropertyDifference(nameof(Bills.BillNo), oldValue.BillNo, null));
changedProperties.Add(new PropertyDifference(nameof(Bills.BillDt), oldValue.BillDt, null));
changedProperties.Add(new PropertyDifference(nameof(Bills.Amt), oldValue.Amt, null));
changedProperties.Add(new PropertyDifference(nameof(Bills.DueDt), oldValue.DueDt, null));
changedProperties.Add(new PropertyDifference(nameof(Bills.PaidOn), oldValue.PaidOn, null));
changedProperties.Add(new PropertyDifference(nameof(Bills.Remarks), oldValue.Remarks, null));
}
else if (newValue != null)
{
id = newValue.id;
changedProperties.Add(new PropertyDifference(nameof(Bills.Party), null, newValue.Party));
changedProperties.Add(new PropertyDifference(nameof(Bills.BillNo), null, newValue.BillNo));
changedProperties.Add(new PropertyDifference(nameof(Bills.BillDt), null, newValue.BillDt));
changedProperties.Add(new PropertyDifference(nameof(Bills.Amt), null, newValue.Amt));
changedProperties.Add(new PropertyDifference(nameof(Bills.DueDt), null, newValue.DueDt));
changedProperties.Add(new PropertyDifference(nameof(Bills.PaidOn), null, newValue.PaidOn));
changedProperties.Add(new PropertyDifference(nameof(Bills.Remarks), null, newValue.Remarks));
}
else
{
return null;
}
if (changedProperties.Count == 0) return null;
return new BillsDifference(id, changedProperties);
}
public static IEnumerable<BillsDifference> FindChanges(IEnumerable<Bills> before, IEnumerable<Bills> after)
{
List<Bills> afterList = after.ToList();
foreach (Bills oldValue in before)
{
Bills newValue = afterList.FirstOrDefault(b => b.Id == oldValue.Id);
if (newValue != null) afterList.Remove(newValue);
BillsDifference diff = Create(oldValue, newValue);
if (diff != null) yield return diff;
}
foreach (Bills newValue in afterList)
{
yield return Create(null, newValue);
}
}
}
List<Bills> before = GetSelectedBillData(x);
bills.UpdateBill(bill);
List<Bills> after = GetSelectedBillData(x);
List<BillDifference> differences = BillDifference.FindChanges(before, after).ToList();
if (differences.Count != 0)
{
}