问题描述
说我有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的匹配行,如果没有<<<<
问题描述
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.