最好的算法在同步两个IList的C#2.0 [英] Best algorithm for synchronizing two IList in C# 2.0
问题描述
想象一下以下类型:
public struct Account
{
public int Id;
public double Amount;
}
什么是同步的最好的算法两个的IList<帐户>
C#2.0中? (无LINQ)?
What is the best algorithm to synchronize two IList<Account>
in C# 2.0 ? (No linq) ?
的第一列表(L1)是参考列表,第二(L2)被一个根据第一同步
The first list (L1) is the reference list, the second (L2) is the one to synchronize according to the first:
- 在L2所有帐户不再present在L1必须从L2被删除
- 在L2所有账户中仍然存在L1必须更新(金额属性)
- 所有帐户是在L 1,但尚未在L2中,必须添加到L2
该ID标识的帐户。这已经不是什么太难找一个天真和工作算法,但我想知道是否有一个聪明的解决方案来处理这种情况下,不会损坏可读性和perfs。
The Id identifies accounts. It's no too hard to find a naive and working algorithm, but I would like to know if there is a smart solution to handle this scenario without ruining readability and perfs.
修改
- 帐户类型没有关系,是可能是一类,具有属性,平等的成员,等等。
- 在L1和L2没有排序
- L 2项不能以L1物品来代替,它们必须被更新(逐场,由属性属性)
推荐答案
一开始我想摆脱可变结构的。可变的值类型是一个根本的坏事。 (由于是公共领域,IMO。)
For a start I'd get rid of the mutable struct. Mutable value types are a fundamentally bad thing. (As are public fields, IMO.)
这可能是值得建立一个字典,所以你可以很容易地比较两个表中的内容。一旦你得到了检查presence /没有那么容易的方法,剩下的应该是简单的。
It's probably worth building a Dictionary so you can easily compare the contents of the two lists. Once you've got that easy way of checking for presence/absence, the rest should be straightforward.
要不过说实话,这听起来像你基本上要L2是L1的完整副本......清晰的L2和只需要调用的AddRange?或者是,你还需要采取其他行动的地步你改变L2时?
To be honest though, it sounds like you basically want L2 to be a complete copy of L1... clear L2 and just call AddRange? Or is the point that you also want to take other actions while you're changing L2?
这篇关于最好的算法在同步两个IList的C#2.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!