0
所以,我有一个树形视图,其中一些行来自DataTable。当我取的数据表,我想用下面的基本规则,以更新树:存在于树,但没有相应的行 DataTable中 查找两个数据表之间的差异
- 删除节点但需要更新 (不同日期时间)
- 插入 树中不存在但在DataTable中执行的节点。
为此我有一个字典,散列一个GUID(在数据表的主键),以树节点:
Dictionary<Guid, NoteNode> MyHashNoteToNode;
,其中NoteNode从树节点衍生的,通过添加一个LastEdit的日期时间字段。到目前为止,如此平庸。
为了找到代表树和DataTable之间区别的3组,我写了下面的代码。我很想听听有一个非常简单的Linq查询,它会返回3组(插入,编辑,删除),或者简单的将为我执行的简短操作,也许是我在某处丢失的一种方法。或者这是非常理想的?
// Find all items that need inserting.
List<DataRow> toInsert = new List<DataRow>();
foreach (DataRow row in MyNotes.Rows)
{
NoteNode node = null;
MyHashNoteToNode.TryGetValue((Guid)row["ID"], out node);
if(node == null)
{
toInsert.Add(row);
}
}
// Find all items that need editing/changing.
List<DataRow> toEdit = new List<DataRow>();
foreach (DataRow row in MyNotes.Rows)
{
NoteNode node = null;
MyHashNoteToNode.TryGetValue((Guid)row["ID"], out node);
if(node != null)
{
if((DateTime)row["Edited"] != node.LastEdit)
{
toEdit.Add(row);
}
}
}
// Find all items that need deleting.
List<NoteNode> toDelete = new List<NoteNode>();
foreach (NoteNode node in MyHashNoteToNode.Values)
{
if (!MyNotes.Rows.Contains(node.Key))
{
toDelete.Add(node);
}
}
}
我喜欢这个。谢谢。 – Robinson