【问题标题】:c# Left join two datatable on multiple columns LINQc#在多列LINQ上左连接两个数据表
【发布时间】:2022-08-09 21:49:16
【问题描述】:

如何根据多列匹配左连接 2 个数据表? 为了比较右表中哪些数据行不匹配

增量上传的一部分需要只从源数据表中引入新行

    标签: c# sql linq .net-core


    【解决方案1】:

    找到了一种使用 LINQ 在 c# 中使用 join 比较两个数据表的方法(左)

     IEnumerable<DataRow> result = (from srcDt in dtSource.AsEnumerable()
                          join dstDt in dtDestination.AsEnumerable()
                          on new { EmployeeID = srcDt["EmployeeID "], Environment = srcDt["Environment"] } equals new { EmployeeID = dstDt["EmployeeID "], Environment = dstDt["Environment"] }
                         into g
                         from row in g.DefaultIfEmpty()
                         where row == null
                         select srcDt);
            // verify if the result has any rows in the dataset
            if (result.Any())
            {
                 DataTable dtInserts = result.CopyToDataTable();
                 // other code which uses the new datarows to perform inserts
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-12
      相关资源
      最近更新 更多