如何从数据集中的两个表中组合和检索多个列 [英] How to combine and retrieve multiple columns from two tables in a Dataset

查看:19
本文介绍了如何从数据集中的两个表中组合和检索多个列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个表的数据集,通过引用 (Loop_id) 连接

I have a DataSet with two tables connected by a reference (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 之外的所有列.结果表应该像

How do I retrieve all the columns, except loop_id from both tables to single table. The resulting table should appear like

T1_Column1   T1_Column2       T2_Column1
1            ItemCode_AAA     2014-Sep-09
2            ItemCode_BBB     2014-Nov-09

我使用的是 Net Framework 4.5

I am using Net Framework 4.5

推荐答案

使用 Linq 到数据集:

要枚举表,请调用 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")});

这篇关于如何从数据集中的两个表中组合和检索多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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