我可以合并两个.NET数据表,而不合并空值,而不必自己循环行吗?

我可以合并两个.NET数据表,而不合并空值,而不必自己循环行吗?,.net,.net,假设我有两个.NET数据表,按如下方式填充,其中ID是主键,null表示DBNull.Value(不是字符串“null”): |表A | |表B |ID | Column1 | Column2 | ID | Column1 | Column2| |1 | foo | 10 | | 1 | zzz |空| |2 | baz | 20 | 2 | null | 99| 我想合并这些表,结果如下: |ID |Column1 |Column2 | |1 |zzz |10

假设我有两个.NET数据表,按如下方式填充,其中ID是主键,
null
表示
DBNull.Value
(不是字符串
“null”
):

|表A | |表B |ID | Column1 | Column2 | ID | Column1 | Column2| |1 | foo | 10 | | 1 | zzz |空| |2 | baz | 20 | 2 | null | 99| 我想合并这些表,结果如下:

|ID |Column1 |Column2 | |1 |zzz |10 | |2 |baz |99 | |ID |第1列|第2列| |1 | zzz | 10| |2 | baz | 99| 换句话说,我想将表B中的数据合并到表A中,就像
DataTable.merge
一样,但是当ID匹配时,我不想复制表B中的整行,我只想复制数据不为null的字段

我可以通过两个表的行编写自己的循环来完成此操作,但我想知道库中是否已经有一个方法,这样我就不必重写自己版本的
DataTable.Merge

您可以尝试以下方法:

public static DataTable Union (DataTable First, DataTable Second)
{

      //Result table
      DataTable table = new DataTable("Union");

      //Build new columns
      DataColumn[] newcolumns = new DataColumn[First.Columns.Count];

      for(int i=0; i < First.Columns.Count; i++)
      {
          newcolumns[i] = new DataColumn(
          First.Columns[i].ColumnName, First.Columns[i].DataType);
      }

      table.Columns.AddRange(newcolumns);
      table.BeginLoadData();

      foreach(DataRow row in First.Rows)
      {
           table.LoadDataRow(row.ItemArray,true);
      }

      foreach(DataRow row in Second.Rows)
      {
          table.LoadDataRow(row.ItemArray,true);
      }

      table.EndLoadData();
      return table;
}
publicstaticdatatable联合(DataTable第一,DataTable第二)
{
//结果表
DataTable=新的DataTable(“联合”);
//新建栏目
DataColumn[]newcolumns=newDataColumn[First.Columns.Count];
for(int i=0;i

来自(未测试)。

老鼠……我认为我找的东西不存在

值得一提的是,如果有人看到这篇文章和我有同样的需求,这就是我最终得到的“滚动我自己的”代码:

        // Merge 
        foreach (DataRow br in tableB.Rows)
        {
            // This assumes a one-column primary key. In my case,
            // that's valid, but not for a general-purpose function.
            DataRow ar = tableA.Rows.Find(br[tableB.PrimaryKey[0]]);
            if (ar == null)
            {
                continue;
                // If I really wanted it to act like DataTable.Merge, it would
                // add the missing row to tableA rather than just skipping it.
                // again, I didn't implement this because I didn't need it.
            }
            foreach (DataColumn ac in tableA.Columns)
            {
                DataColumn bc = tableB.Columns[ac.ColumnName];
                if (bc != null && !br.IsNull(bc))
                    ar[ac] = br[bc];
            }
        }

正如注释所指出的,这并没有完成DataTable.Merge所做的全部工作,并且做出了一些通常无效的假设。不过,解决这些问题并不难。我把它留在这里,以防它对某人有所帮助。

不,这不起作用……正如所写的那样,它只在底部添加第二个表的行,而不更新具有相同PK的行(因为您没有将主键添加到“Union”表)。在添加主键后,我尝试使用LoadDataRow,但这与DataTable.Merge的作用相同——它使用第二个表中的所有值(包括空值)更新整行。我自己滚了…哦,好吧。
        // Merge 
        foreach (DataRow br in tableB.Rows)
        {
            // This assumes a one-column primary key. In my case,
            // that's valid, but not for a general-purpose function.
            DataRow ar = tableA.Rows.Find(br[tableB.PrimaryKey[0]]);
            if (ar == null)
            {
                continue;
                // If I really wanted it to act like DataTable.Merge, it would
                // add the missing row to tableA rather than just skipping it.
                // again, I didn't implement this because I didn't need it.
            }
            foreach (DataColumn ac in tableA.Columns)
            {
                DataColumn bc = tableB.Columns[ac.ColumnName];
                if (bc != null && !br.IsNull(bc))
                    ar[ac] = br[bc];
            }
        }