我正在LazySave<T>
我的基础数据访问层 ( BaseDAL
) 中为对象数据库创建一个。
public void LazySave<T>(IEnumerable<T> TList, Func<T, T, bool> condition, IEqualityComparer<T> comparer) where T : class
{
//Select where GetOne<T> returns null, meaning this item doesn't exist
var itemsToStore = TList.Where(TItem => GetOne<T>(e => condition(TItem, e)) == null);
//Select where GetOne<T> returns not null, meaning this item exists
var itemsToUpdate = TList.Where(TItem => GetOne<T>(e => condition(TItem, e)) != null);
//Get all of the items
var allItems = GetMany<T>();
//Any items which aren't in TList but are in the original list need to be deleted
var itemsToDelete = TList.Intersect(allItems, comparer);
itemsToStore.ToList().ForEach(i => Store<T>(i));
itemsToUpdate.ToList().ForEach(i => Update<T>(i, condition));
itemsToDelete.ToList().ForEach(i => Delete<T>(i, condition));
}
我将以下对象和比较器传递给该方法。
public class BankHoliday : IBankHoliday, IEquatable<BankHoliday>
{
public DateTime Date { get; set; }
public bool Equals(BankHoliday other)
{
return other.Date == Date;
}
}
public class BankHolidayComparer : IEqualityComparer<BankHoliday>
{
public bool Equals(BankHoliday x, BankHoliday y)
{
return x.Date == y.Date;
}
public int GetHashCode(BankHoliday obj)
{
if (object.ReferenceEquals(obj, null)) return 0;
return obj.Date.GetHashCode();
}
}
称为:
var comparer = (IEqualityComparer<BankHoliday>)new BankHolidayComparer();
bhDAL.LazySave<BankHoliday>(holidays, ((T, T2) => T.Date == T2.Date), comparer);
数据库中的列表有 11 个日期。我删除一个然后调用LazySave
,将 10 个项目传递给TList
但是Intersect
没有返回结果,甚至没有命中Equals
or GetHashCode
。任何想法>我认为这与我的比较器有关...