LINQ返回加入中所有表的所有列[英] Linq return all columns from all tables in the join

本文是小编为大家收集整理的关于LINQ返回加入中所有表的所有列的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

说我有2个表都包含动态列,我希望在执行左外连接后,从两个表中检索所有列的数据集(后来我将其绑定到网格视图).

示例查询:

var query = from TableA in ds.Tables[0].AsEnumerable()
            join TableB in ds.Tables[1].AsEnumerable() on new { col1 = TableA.Field<Int32>("colA"), col2 = TableA.Field<DateTime>("colB") }
            equals new { col1 = TableB.Field<Int32>("colA"), col2 = TableB.Field<DateTime>("colB") }
            into GJ
            from sub in GJ.DefaultIfEmpty()
            select TableA;

问题: 我想一起选择tablea和tableb.上面的示例查询起作用,左外连接后,它填充了所有列表.但是我希望从两个表中检索所有列.请指教.

推荐答案

只需在匿名类型中选择两个部分:

var query = from TableA in ds.Tables[0].AsEnumerable()
            join TableB in [ ...] on [...] equals [...] into GJ
            from sub in GJ.DefaultIfEmpty()
            select new { RowA = TableA, RowB = sub };

结果的每个元素将具有两个属性:RowA是TableA的一行,而RowB是TableB 或 null的匹配行,如果没有<<<<> C3>匹配RowA.

本文地址:https://www.itbaoku.cn/post/1556912.html

问题描述

Lets say I have 2 tables both of them contain dynamic columns and I wish to retrieve a collection of datarow with all the columns from both the tables(later i will bind it to a grid view) after performing left outer join.

Sample Query:

var query = from TableA in ds.Tables[0].AsEnumerable()
            join TableB in ds.Tables[1].AsEnumerable() on new { col1 = TableA.Field<Int32>("colA"), col2 = TableA.Field<DateTime>("colB") }
            equals new { col1 = TableB.Field<Int32>("colA"), col2 = TableB.Field<DateTime>("colB") }
            into GJ
            from sub in GJ.DefaultIfEmpty()
            select TableA;

Problem: I want to select tableA and tableB together. The above sample query works and it populates all columns of tableA after left outer join. But i wish to retrieve all the columns from both the tables. Please advice.

推荐答案

Just select both parts into an anonymous type:

var query = from TableA in ds.Tables[0].AsEnumerable()
            join TableB in [ ...] on [...] equals [...] into GJ
            from sub in GJ.DefaultIfEmpty()
            select new { RowA = TableA, RowB = sub };

Each element of the result will have two properties: RowA being a row from TableA, and RowB being a matching row from TableB or null if no rows from TableB matches RowA.