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

查看:236
本文介绍了Linq返回联接中所有表的所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

样本查询:

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.上面的示例查询有效,并且在左外部联接之后填充了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 };

结果的每个元素将具有两个属性:RowATableA的行,RowBTableB 的匹配行;如果TableB匹配RowA.

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.

这篇关于Linq返回联接中所有表的所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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