我在基础数据访问层(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
或GetHashCode
。任何想法&gt;我认为这与我的比较器有关...
答案 0 :(得分:0)
根据您的评论,您误解了Intersect
的目的:
//Any items which aren't in TList but are in the original list need to be deleted
var itemsToDelete = TList.Intersect(allItems, comparer);
Intersect
在两个序列中找到 的项目。根据你的评论,我怀疑你想要Except
:
var itemsToDelete = allItems.Except(TList, comparer);
如果在使用Equals
时未调用您的自定义GetHashCode
和Intersect
方法,则表明 allItems
或 TList
(顺便说一句是奇数参数名称)是空的。
答案 1 :(得分:0)
我相信IEqualityComparer是用于比较对象实例的外部类吗?
在BankHoliday类上,尝试使用覆盖实现IEquatable:
public override bool Equals(object obj)
和
public override int GetHashCode()
编辑:并使用@ Jon的除外代码示例。
答案 2 :(得分:0)
这可能是一个愚蠢的问题,但只是为了确认 - 你是将你的断点放在比较器的Equals和GetHashCode上? (不是BankHoliday的方法)
由于交叉点的延迟评估,在击中断点之前必须调用下面的行。
itemsToDelete.ToList().ForEach(i => Delete<T>(i, condition));