数据表比较 - 主键不起作用
Datatable Comparison of - primary keys is not working
我写了一个代码块,比较数据表的方案差异。
这是代码:
private static void ValidateSchema(DataTable originalTbl, DataTable otherTbl)
{
var primaryKeyDoesNotMatch = originalTbl.PrimaryKey != otherTbl.PrimaryKey;
if(primaryKeyDoesNotMatch)
{
throw new ArgumentException("primary key does not match");
}
var primaryKeyDoesNotExist = originalTbl.PrimaryKey == null;
if(primaryKeyDoesNotExist)
{
throw new ArgumentException("primary key does not exist");
}
var otherTableHasAdditionalColumns = (from x in otherTbl.Columns.OfType<DataColumn>() where !originalTbl.Columns.OfType<DataColumn>().Any(y => y.ColumnName == x.ColumnName) select x).Any();
if (otherTableHasAdditionalColumns)
{
throw new ArgumentException("Other table does have additional columns.");
}
var columnsAreMissingInOtherTable = (from x in originalTbl.Columns.OfType<DataColumn>() where !otherTbl.Columns.OfType<DataColumn>().Any(y => y.ColumnName == x.ColumnName) select x).Any();
if (columnsAreMissingInOtherTable)
{
throw new ArgumentException("Other table does not have all the columns.");
}
var columnDataTypesDoesNotMatch = (from x in otherTbl.Columns.OfType<DataColumn>() where originalTbl.Columns.OfType<DataColumn>().Any(y => x.DataType != y.DataType) select x).Any();
if (columnDataTypesDoesNotMatch)
{
throw new ArgumentException("Column's data type does not match");
}
}
我也对此进行了单元测试,已实施以测试所有这些场景。
问题是,即使我测试 "columnDataTypeDoesNotMatch" 或 "columnsAreMissinginOtherTable",它也会运行到第一个 IF 语句并告诉我 "primary key does not match" 但是它们会怎样!
知道为什么会这样吗?
感谢您的帮助
提前汇款。
当然可以。 PrimaryKey
永远不可能彼此相等(好吧,除非它们是相同的 table,或者它们都有 null
- 尽管它仍然应该有两个单独的 new DataColumn[0]
数组),因为您正在执行参考比较,并通过阵列进行引导。
相反,您必须根据所涉及的列的名称(以及根据您的要求、数据类型)进行检查:
bool Compare(DataColumn[] primary, DataColumn[] secondary)
{
if (primary.Length != secondary.Length) return false;
var names = new HashSet<string>(secondary.Select(col => col.ColumnName));
return primary.All(col => names.Contains(col.ColumnName));
}
您必须根据您的要求扩展这些内容,例如根据您是否关心区分大小写等。
我写了一个代码块,比较数据表的方案差异。
这是代码:
private static void ValidateSchema(DataTable originalTbl, DataTable otherTbl)
{
var primaryKeyDoesNotMatch = originalTbl.PrimaryKey != otherTbl.PrimaryKey;
if(primaryKeyDoesNotMatch)
{
throw new ArgumentException("primary key does not match");
}
var primaryKeyDoesNotExist = originalTbl.PrimaryKey == null;
if(primaryKeyDoesNotExist)
{
throw new ArgumentException("primary key does not exist");
}
var otherTableHasAdditionalColumns = (from x in otherTbl.Columns.OfType<DataColumn>() where !originalTbl.Columns.OfType<DataColumn>().Any(y => y.ColumnName == x.ColumnName) select x).Any();
if (otherTableHasAdditionalColumns)
{
throw new ArgumentException("Other table does have additional columns.");
}
var columnsAreMissingInOtherTable = (from x in originalTbl.Columns.OfType<DataColumn>() where !otherTbl.Columns.OfType<DataColumn>().Any(y => y.ColumnName == x.ColumnName) select x).Any();
if (columnsAreMissingInOtherTable)
{
throw new ArgumentException("Other table does not have all the columns.");
}
var columnDataTypesDoesNotMatch = (from x in otherTbl.Columns.OfType<DataColumn>() where originalTbl.Columns.OfType<DataColumn>().Any(y => x.DataType != y.DataType) select x).Any();
if (columnDataTypesDoesNotMatch)
{
throw new ArgumentException("Column's data type does not match");
}
}
我也对此进行了单元测试,已实施以测试所有这些场景。
问题是,即使我测试 "columnDataTypeDoesNotMatch" 或 "columnsAreMissinginOtherTable",它也会运行到第一个 IF 语句并告诉我 "primary key does not match" 但是它们会怎样!
知道为什么会这样吗?
感谢您的帮助
提前汇款。
当然可以。 PrimaryKey
永远不可能彼此相等(好吧,除非它们是相同的 table,或者它们都有 null
- 尽管它仍然应该有两个单独的 new DataColumn[0]
数组),因为您正在执行参考比较,并通过阵列进行引导。
相反,您必须根据所涉及的列的名称(以及根据您的要求、数据类型)进行检查:
bool Compare(DataColumn[] primary, DataColumn[] secondary)
{
if (primary.Length != secondary.Length) return false;
var names = new HashSet<string>(secondary.Select(col => col.ColumnName));
return primary.All(col => names.Contains(col.ColumnName));
}
您必须根据您的要求扩展这些内容,例如根据您是否关心区分大小写等。