c#:检查datarow是否存在于具有动态列的不同数据表中



我想检查一个数据表中的DataRow是否存在于另一个数据表中。

我有两个数据表。会有一些相同的datarow存在于两者中。如果我知道每次会有哪些列,这将是如此容易,但我不知道。使用…

datatable.Rows[i].Field <String> columnName);

将不起作用,因为该字段可以是整数、字符串或日期/时间。但是,我知道这两个文件具有相同的列名。

我的代码是零零碎碎的,但这是目前为止的样子。现在我生成一个列名列表,但仅此而已。

for (var h = 0; h < origDbfFile.datatable.Columns.Count; h++) {
     columnNames.Add(origDbfFile.datatable.Columns[h].ColumnName);
}

我有一个循环噩梦,如果有人能找到一个更干净的解决方案,这将是非常感激!

for (int g = 0; g < origDbfFile.dataset.Tables[0].Rows.Count; g++)
{
     for (int h = 0; i < modDbfFile.dataset.Tables[0].Columns.Count; h++)
     {
           foreach (String columnName in columnNames)
           {
           String rowValue = origDbfFile.dataset.Tables[0].Rows[g].Field<String>(Convert.ToString(columnName));
           //test data
           result += "Column name: &nbsp: " + columnName + "<br/>";
           result += "Value &nbsp; " + rowValue + "<br/><br/>";
           //if logic will go below
            }
      }
 }

假设您对行值的装箱/拆箱和值/引用类型没有问题,那么您的代码几乎就是您在给定情况下所能做的一切。

当然,如果你对linq和extension方法感兴趣,你可以把它做得更漂亮

public static class DataRowExtensions
{
    public static int IndexIn(this DataRow thisRow, DataTable table)
    {
        return table.Rows
            .OfType<DataRow>()
            .Select((row, i) => new { row, index = i + 1 })
            .Where(pair => EqCondition(thisRow, pair.row))
            .Select(pair => pair.index)
            .FirstOrDefault() - 1;
    }
    public static bool EqCondition(DataRow row1, DataRow row2)
    {
        // check for the equality of row values
        return true;
    }
}
...
for (int i = 0; i < tab1.Rows.Count; i++)
{
    var index = tab1.Rows[i].IndexIn(tab2);
    if (index < 0)
    {
        Console.WriteLine("The row at index {0} was not found in second table", i);
    }
    else
    {
        Console.WriteLine("The row at index {0} was found in second table at index", i, index);
    }
}

但是除了在找到第一个匹配后中断之外,代码是完全相同的。

我的建议是使用id相等,但要记住,大多数时候数据来自带索引的数据源,您可能会发现自己在这种情况下。

p。在你的第二个你有两个错误

  • 我想你应该使用。Rows. count

我的解决方案是抓取值不作为整数,字符串或日期/时间…而是作为一个通用对象。然后,我可以将其转换为字符串,然后将其作为字符串进行比较。

//check each column name for a change
foreach (String columnName in columnNames)
{
    //this grabs whatever value is in that field                            
    String origRowValue = "" + origRow.Field<Object>(columnName);
    String modRowValue = "" + modRow.Field<Object>(columnName);
    //check if they are the same
    if (origRowValue.Equals(modRowValue))
    {
        //if they aren the same, increase the number matched by one
        numberMatched++;
        //add the column to the list of columns that don't match
    }
    else
    {
        mismatchedColumns.Add(columnName);
    }
}

聪明的解决方案还是什么?谢谢大家的帮助:)

如果你使用的是。net 3.5或更高版本,类似这样的代码将为你提供两个表中存在的列名列表。

var columns = origDbfFile.datatable.Columns
    .Select(o => o.ColumnName)
    .Intersect(modDbfFile.datatable.Columns
               .Select(m => m.ColumnName));

虽然我不确定这解决了你的整体问题。你能再澄清一点吗?

最新更新