43 lines
1.2 KiB (Stored with Git LFS)
C#
43 lines
1.2 KiB (Stored with Git LFS)
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
public static class GoogleSheetComparer
|
|
{
|
|
public static GoogleSheetDiffResult Compare<T>(List<T> oldList, List<T> newList) where T : class
|
|
{
|
|
var result = new GoogleSheetDiffResult();
|
|
|
|
var oldDict = oldList.ToDictionary(x => GetId(x));
|
|
var newDict = newList.ToDictionary(x => GetId(x));
|
|
|
|
foreach (var newId in newDict.Keys)
|
|
{
|
|
if (!oldDict.ContainsKey(newId)) result.Added.Add(newId);
|
|
else if (!IsSame(oldDict[newId], newDict[newId])) result.Modified.Add(newId);
|
|
}
|
|
|
|
foreach (var oldId in oldDict.Keys)
|
|
{
|
|
if (!newDict.ContainsKey(oldId)) result.Removed.Add(oldId);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
private static string GetId(object obj)
|
|
{
|
|
var field = obj.GetType().GetField("Id");
|
|
return field?.GetValue(obj)?.ToString() ?? "";
|
|
}
|
|
|
|
private static bool IsSame(object a, object b)
|
|
{
|
|
foreach (var field in a.GetType().GetFields())
|
|
{
|
|
var valA = field.GetValue(a);
|
|
var valB = field.GetValue(b);
|
|
if (!Equals(valA, valB)) return false;
|
|
}
|
|
return true;
|
|
}
|
|
} |