全外连接,2数据表,以列清单 [英] Full outer join, on 2 data tables, with a list of columns

查看:107
本文介绍了全外连接,2数据表,以列清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数据表,我不知道他们的数据列的列表。此列表必须在运行时进行提取,用于完全外连接。

I have 2 data tables, which I do not know their list of data columns. This list must be extracted at run time, and be used for the full outer join.

在使用这些列,2个表之间的列需要合并的,我需要显示的所有数据。

When using these columns, the columns between the 2 tables need to be merged, and I need all data to be displayed.

直到现在我做的是

  1. 在获取公共列,使用相交,并实施的IEqualityComparer
  2. 创建一个新的数据表,这些列,从而使2数据表将被合并到这个新表

不过,我有问题,使用LINQ,在第2步。

However, I am having issues with Linq, on the 2nd step.

到目前为止我有:

获取公共列



    // Get common columns
    var commonColumns = dt1.Columns.OfType().Intersect(dt2.Columns.OfType(), new DataColumnComparer());

创建新的数据表



    // Create the result which is going to be sent to the user
    DataTable result = new DataTable();

    // Add all the columns from both tables
    result.Columns.AddRange(
    dt1.Columns.OfType()
    .Union(dt2.Columns.OfType(), new DataColumnComparer())
    .Select(c => new DataColumn(c.Caption, c.DataType, c.Expression, c.ColumnMapping)).ToArray());

我怎样才能获得一个有效的完整外部动态加入,从datacolumns的名单,即提取在运行时?

How can I obtain an efficient full outer join dynamically, from the List of datacolumns, that is extracted at run time?

推荐答案

这可能为你工作。

var commonColumns = dt1.Columns.OfType<DataColumn>().Intersect(dt2.Columns.OfType<DataColumn>(), new DataColumnComparer());
        DataTable result = new DataTable();

        dt1.PrimaryKey = commonColumns.ToArray();

        result.Merge(dt1, false, MissingSchemaAction.AddWithKey);
        result.Merge(dt2, false, MissingSchemaAction.AddWithKey);

这篇关于全外连接,2数据表,以列清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆