【问题标题】:merge two datatable arrays C#合并两个数据表数组C#
【发布时间】:2013-06-06 21:59:51
【问题描述】:

我有两个数据表数组

  DataTable[] DTrightSplitH2;
  DataTable[] DTleftSplitH2;

我尝试做的是获取 DTright 中的每个数据表并与“key”列中的 DTleft 进行比较 如果它们相同,则合并行

我知道我应该使用 DataTable.Merge 并将 bool 设置为 false 并添加缺少的架构,但我无法让它像我想要的那样工作

【问题讨论】:

    标签: c# join merge


    【解决方案1】:

    试试这个:

    DTrightSplitH2.Union(DTleftSplitH2);
    

    【讨论】:

      【解决方案2】:

      听起来你可以使用我的MergeAll 方法(用法如下)。

      public static DataTable MergeAll(this IList<DataTable> tables, String primaryKeyColumn)
      {
          if (!tables.Any())
              throw new ArgumentException("Tables must not be empty", "tables");
          if(primaryKeyColumn != null)
              foreach(DataTable t in tables)
                  if(!t.Columns.Contains(primaryKeyColumn))
                      throw new ArgumentException("All tables must have the specified primarykey column " + primaryKeyColumn, "primaryKeyColumn");
      
          if(tables.Count == 1)
              return tables[0];
      
          DataTable table = new DataTable("TblUnion");
          table.BeginLoadData(); // Turns off notifications, index maintenance, and constraints while loading data
          foreach (DataTable t in tables)
          {
              foreach (DataColumn col in t.Columns) 
                  col.ReadOnly = false; // required e.g. if you use a DataSet with Foreign-Key Constraints
              table.Merge(t); // same as table.Merge(t, false, MissingSchemaAction.Add);
          }
          table.EndLoadData();
      
          if (primaryKeyColumn != null)
          {
              // since we might have no real primary keys defined, the rows now might have repeating fields
              // so now we're going to "join" these rows ...
              var pkGroups = table.AsEnumerable()
                  .GroupBy(r => r[primaryKeyColumn]);
              var dupGroups = pkGroups.Where(g => g.Count() > 1);
              foreach (var grpDup in dupGroups)
              { 
                  // use first row and modify it
                  DataRow firstRow = grpDup.First();
                  foreach (DataColumn c in table.Columns)
                  {
                      if (firstRow.IsNull(c))
                      {
                          DataRow firstNotNullRow = grpDup.Skip(1).FirstOrDefault(r => !r.IsNull(c));
                          if (firstNotNullRow != null)
                              firstRow[c] = firstNotNullRow[c];
                      }
                  }
                  // remove all but first row
                  var rowsToRemove = grpDup.Skip(1);
                  foreach(DataRow rowToRemove in rowsToRemove)
                      table.Rows.Remove(rowToRemove);
              }
          }
      
          return table;
      }
      

      用法:

      var tables = DTrightSplitH2.Concat(DTleftSplitH2).ToArray();
      DataTable TblUnion = tables.MergeAll("key");
      

      【讨论】:

      • 每张表都有一个键
      • 那么你应该展示一些表格的结构和期望的结果的例子。
      猜你喜欢
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 2020-10-13
      • 2011-10-13
      相关资源
      最近更新 更多