我有一个DataSet,其中两个表通过引用(Loop_id)连接
Table1
Column1 Column2 Loop_id
1 ItemCode_AAA 6
2 ItemCode_BBB 8
Table2
Column1 Loop_id
2014-Sep-09 6
2014-Nov-09 8
如何从两个表到单个表中检索除loop_id之外的所有列。 结果表应该显示为
T1_Column1 T1_Column2 T2_Column1
1 ItemCode_AAA 2014-Sep-09
2 ItemCode_BBB 2014-Nov-09
我正在使用Net Framework 4.5
答案 0 :(得分:1)
要枚举表格,请致电AsEnumerable
。
DataTable table1 = ds.Tables["table1"];
DataTable table2 = ds.Tables["table2"];
var records = (from t1 in table1.AsEnumerable()
join t2 in table2.AsEnumerable()
on t1.Field<int>("Loop_id") equals t2.Field<int>("Loop_id")
select new {T1_Column1 = t1.Field<string>("Column1"),
T2_Column2 = t2.Field<string>("Column2"),
T2_Column1 = t2.Field<string>("Column1")});
答案 1 :(得分:0)
使用View
或者你可以使用join
SELECT T1.Column1 AS T1_Col , T1.Column2 AS T1_Col , T2.Column1 AS T2_Col
FROM dbo.Table1 AS T1 RIGHT OUTER JOIN
dbo.Table2 AS T2 ON dbo.T1.Loop_id = dbo.T2.Loop_id