C# 比较两个表的最快方法是什么?
例如,有两个具有相同架构但内容不同的表: 表1C# 比较两个表的最快方法是什么?,c#,comparison,C#,Comparison,例如,有两个具有相同架构但内容不同的表: 表1 | field1 | field2 | field3 | ---------------------------------------- | 1 | aaaaa | 100 | | 2 | bbbbb | 200 | | 3 | ccccc | 300 | | 4 | ddddd
| field1 | field2 | field3 |
----------------------------------------
| 1 | aaaaa | 100 |
| 2 | bbbbb | 200 |
| 3 | ccccc | 300 |
| 4 | ddddd | 400 |
表2
| field1 | field2 | field3 |
----------------------------------------
| 2 | xxxxx | 200 |
| 3 | ccccc | 999 |
| 4 | ddddd | 400 |
| 5 | eeeee | 500 |
预期的比较结果将是: 在B中删除:
| 1 | aaaaa | 100 |
Table1:| 2 | bbbbb | 200 |
Table2:| 2 | xxxxx | 200 |
Table1:| 3 | ccccc | 300 |
Table2:| 3 | ccccc | 999 |
不匹配:
| 1 | aaaaa | 100 |
Table1:| 2 | bbbbb | 200 |
Table2:| 2 | xxxxx | 200 |
Table1:| 3 | ccccc | 300 |
Table2:| 3 | ccccc | 999 |
B中新增的
| 5 | eeeee | 500 |
使用C#,比较两个表的最快方法是什么
目前我的实施是:
检查表1中的每一行是否与表2中的完全匹配;
检查表2中的每一行是否与表1中的完全匹配
效率是n*n
,因此对于100k行,在服务器上运行需要20分钟
非常感谢你可以试试这样的东西,应该很快:
class objType
{
public int Field1 { get; set; }
public string Field2 { get; set; }
public int Field3 { get; set; }
public bool AreEqual(object other)
{
var otherType = other as objType;
if (otherType == null)
return false;
return Field1 == otherType.Field1 && Field2 == otherType.Field2 && Field3 == otherType.Field3;
}
}
var tableOne = new objType[] {
new objType { Field1 = 1, Field2 = "aaaa", Field3 = 100 },
new objType { Field1 = 2, Field2 = "bbbb", Field3 = 200 },
new objType { Field1 = 3, Field2 = "cccc", Field3 = 300 },
new objType { Field1 = 4, Field2 = "dddd", Field3 = 400 }
};
var tableTwo = new objType[] {
new objType { Field1 = 2, Field2 = "xxxx", Field3 = 200 },
new objType { Field1 = 3, Field2 = "cccc", Field3 = 999 },
new objType { Field1 = 4, Field2 = "dddd", Field3 = 400 },
new objType { Field1 = 5, Field2 = "eeee", Field3 = 500 }
};
var originalIds = tableOne.ToDictionary(o => o.Field1, o => o);
var newIds = tableTwo.ToDictionary(o => o.Field1, o => o);
var deleted = new List<objType>();
var modified = new List<objType>();
foreach (var row in tableOne)
{
if(!newIds.ContainsKey(row.Field1))
deleted.Add(row);
else
{
var otherRow = newIds[row.Field1];
if (!otherRow.AreEqual(row))
{
modified.Add(row);
modified.Add(otherRow);
}
}
}
var added = tableTwo.Where(t => !originalIds.ContainsKey(t.Field1)).ToList();
class对象类型
{
公共int字段1{get;set;}
公共字符串字段2{get;set;}
公共int字段3{get;set;}
公共布尔值相等(对象其他)
{
var otherType=其他对象类型;
if(otherType==null)
返回false;
返回Field1==otherType.Field1&&Field2==otherType.Field2&&Field3==otherType.Field3;
}
}
var tableOne=新对象类型[]{
新对象类型{Field1=1,Field2=“aaaa”,Field3=100},
新对象类型{Field1=2,Field2=“bbbb”,Field3=200},
新对象类型{Field1=3,Field2=“cccc”,Field3=300},
新对象类型{Field1=4,Field2=“dddd”,Field3=400}
};
var tableTwo=新对象类型[]{
新对象类型{Field1=2,Field2=“xxxx”,Field3=200},
新对象类型{Field1=3,Field2=“cccc”,Field3=999},
新的对象类型{Field1=4,Field2=“dddd”,Field3=400},
新对象类型{Field1=5,Field2=“eeee”,Field3=500}
};
var originalIds=tableOne.ToDictionary(o=>o.Field1,o=>o);
var newIds=tableTwo.ToDictionary(o=>o.Field1,o=>o);
var deleted=新列表();
var modified=新列表();
foreach(表一中的var行)
{
如果(!newIds.ContainsKey(row.Field1))
删除。添加(行);
其他的
{
var otherRow=newIds[row.Field1];
如果(!otherRow.AreEqual(行))
{
修改。添加(行);
修改。添加(其他行);
}
}
}
var added=tableTwo.Where(t=>!originalIds.ContainsKey(t.Field1)).ToList();
可能值得重写
Equals
而不是AreEqual
(或者使AreEqual
成为类定义之外的助手方法),但这取决于项目的设置方式。它们是数据表吗?如果是,定义(主)键并进行比较。