我想比较两个表。如果x行和y行不相等,我想得到一个类似等于/不相等的输出。所以所有可能的事情都应该报告,但我不知道最好的方法是什么。我只需要一些例子来获得更好的想法:
我的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
}
}
}
我建议使用HashSet<String>
,而不是比较数组(问题中的String[] Keys
),因为基于哈希的搜索比O(N)更快-O(1);像这样:
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:
}