问题描述
我有 2 个数据表,我不知道它们的数据列列表.此列表必须在运行时提取,并用于完全外连接.
使用这些列时,需要合并2个表之间的列,我需要显示所有数据.
到目前为止,我正在做的是
- 获取常用列,使用 intersect,并实现 IEqualityComparer
- 使用这些列创建一个新数据表,以便将 2 个数据表合并到这个新表中
但是,我在第二步遇到了 Linq 问题.
到现在为止:
获取常用列
<上一页>//获取常用列var commonColumns = dt1.Columns.OfType().Intersect(dt2.Columns.OfType(), new DataColumnComparer());新建数据表
<上一页>//创建要发送给用户的结果数据表结果 = 新数据表();//添加两个表中的所有列结果.Columns.AddRange(dt1.Columns.OfType().Union(dt2.Columns.OfType(), new DataColumnComparer()).Select(c => new DataColumn(c.Caption, c.DataType, c.Expression, c.ColumnMapping)).ToArray());如何从运行时提取的数据列列表中动态获取高效的完全外连接?
这可能对你有用
var commonColumns = dt1.Columns.OfType().Intersect(dt2.Columns.OfType(), new DataColumnComparer());数据表结果 = 新数据表();dt1.PrimaryKey = commonColumns.ToArray();result.Merge(dt1, false, MissingSchemaAction.AddWithKey);result.Merge(dt2, false, MissingSchemaAction.AddWithKey);
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.
When using these columns, the columns between the 2 tables need to be merged, and I need all data to be displayed.
Till now what I am doing is
- Get common columns, using intersect, and implementing IEqualityComparer
- Create a new datatable, with these columns, so that the 2 datatables will be merged into this new table
However, I am having issues with Linq, on the 2nd step.
Till now I have :
Get common columns
// Get common columns var commonColumns = dt1.Columns.OfType().Intersect(dt2.Columns.OfType(), new DataColumnComparer());
Create new data table
// 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());
How can I obtain an efficient full outer join dynamically, from the List of datacolumns, that is extracted at run time?
This might work for you
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 个数据表上,带有列列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!