我在c#做一个除了两个DataTable的替代方式。我已超负荷的IEqualityComparer最快方式或自定义的枚举计数元件键入C#
public bool Equals(T sourceRec, T targetRec)
{
string srcHash, tgtHash;
srcHash = HashGenerator.GenerateHash<T>(sourceRec);
tgtHash = HashGenerator.GenerateHash<T>(targetRec);
return srcHash.Equals(tgtHash);
}
public int GetHashCode(T obj)
{
return base.GetHashCode();
}
和除了将通过考虑每个DataRow的哈希值的工作。
public class CompareResult<T>
{
public IEnumerable<T> Inserted_Updated;
public IEnumerable<T> Updated;
public IEnumerable<T> Deleted;
}
public CompareResult<DataRow> Compare(DataTable source, DataTable target)
{
CompareResult<DataRow> rest = new CompareResult<DataRow>();
rest.Inserted_Updated = target.AsEnumerable().Except(source.AsEnumerable(), RowHashComparer);
rest.Deleted = (source.AsEnumerable().Except(target.AsEnumerable(), RowHashComparer)).Except(target.AsEnumerable().Except(source.AsEnumerable(), RowHashComparer));
return rest;
}
CompareResult<DataRow> strResult = SD1.Compare(src, tgt);
RowHashComparer是用来执行自定义属性,除了通过比较哈希代码。 我追查的是等式比较在Except方法返回后实际发生。
请建议。
而不是返回一个'Enumerable',你可以返回'的HashSet <> '。 – Rob
我有一个装满弹珠的包,我想知道我有多少个弹珠。你能告诉我一种不涉及*一个一个地计算*的方法吗?呃,不,不是。也许你应该退后一步,问自己一袋是你真正需要的。其他类型的收藏品是否做得更好? – InBetween
@InBetween - 货币可以通过加权来计算。标准化的大理石在一个已知重量的袋子里可以用一个比例来计数。 – Moho