我想比较两张桌子。如果行x和y不相等,我想获得像Equal / Unequal这样的输出。所以应该报告一切可能,但我不知道最好的方法是什么。我只需要一些例子来获得更好的想法:
我的TableModel
课程:
class TableModel {
private string tableName;
private string[] headers;
private string[] keys;
public int RowCount { get; set; }
public string TableName { get { return tableName; } set { this.tableName = value; } }
public string[] Headers { get { return headers; } set { this.headers = value; } }
public string[] Keys { get { return keys; } set { this.keys = value; } }
}
我的Compare
课程:
private void StartCompare() {
int counter = 0;
foreach(string nkey in newModel.Keys){
foreach(string ckey in currentModel.Keys){
if(!nkey.Equals(ckey)){
counter++;
}
}
if(currentModel.Keys.Length -1 != counter){
//row not found in currentModel
}
}
}
答案 0 :(得分:0)
我建议使用String[] Keys
而不是比较问题中的数组(HashSet<String>
,因为基于散列的搜索更快 - O(1)与O(N)相比;那样:
HashSet<String> newKeys = new HashSet<String>(newModel.Keys);
HashSet<String> currentKeys = new HashSet<String>(currentModel.Keys);
foreach (String key in newKeys)
if (!currentKeys.Contains(key)) {
// The key is in the new model, but not in the current one
//TODO: put the relevant code here:
}
foreach (String key in currentKeys)
if (!newKeys.Contains(key)) {
// The key is in the current model, but not in the new one
//TODO: put the relevant code here:
}