请建议在大小(数据集中的表数)未知的数据集中连接数据表的方法 [英] Please suggest ways to join data tables in a dataset whose size(number of tables in dataset) is unknown
问题描述
我在运行时将excel文件表作为数据集中的数据表。现在我必须根据列表中的条件加入所有这些数据表。 (列表包含用户为了执行左连接而选择的列名...,选择..... on(List1.first item in List == table2.second item in List)。我在Windows中遇到此问题使用C#表单应用程序,我正在使用VS2012。
I am adiing excel file sheets as datatables in a dataset at runtime. Now i have to join all these data tables based on the conditions present in a list. (List contains column names that the user choose inorder to perform a left join..i.e, select.....on (table1.first item in List==table2.second item in List). I am facing this issue in Windows Form Application using C# and I am using VS2012.
推荐答案
如果要加入来自不同工作簿的数据,基本思路就在这里:如何:使用One OleDbConnection从多个工作簿中获取数据? [ ^ ]。
您没有提供将数据提取到数据集的方法。如果是单个工作簿,您可以使用OleDbConnection [ ^ ]和 OleDbCommand [ ^ ]实现这一点。
正确查询应该看起来例如:
If you want to join data from different workbooks, the basic idea is here: How to: Get Data from Multiple Workbooks using One OleDbConnection?[^].
You did not provided a way you're fetching data into dataset. If it's a single workbook, you can use OleDbConnection[^] and OleDbCommand[^] to achieve that.
Proper query should looks like:
SELECT <FieldList>
FROM [Sheet1
AS t1 INNER JOIN [Sheet2
AS t1 INNER JOIN [Sheet2
AS t2 ON t1.Field1 = t2.Field2
AS t2 ON t1.Field1 = t2.Field2
这就是全部。
请参阅:查询表达式语法示例:连接运算符(LINQ to DataSet) [ ^ ]
That's all.
Please, see this: Query Expression Syntax Examples: Join Operators (LINQ to DataSet)[^]
var qry = from x in dtset.Tables(1).AsEnumerable() join y in dtset.Tables(2).AsEnumerable() on x.Field<Type>("Field1") equals y.Field<Type>("Field2")
select new {
NewField1 = x.Field<Type>(Field1),
NewField2 = x.Field<Type>(Field2),
NewField3 = y.Field<Type>(Field1),
NewField4 = y.Field<Type>(Field2)
};
foreach(var c in qry)
{
Console.WriteLine("{0} {1} {2} [3}", c.NewField1, c.NewField2, c.NewField3, c.NewField4);
}
这篇关于请建议在大小(数据集中的表数)未知的数据集中连接数据表的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!