如何将条件放在数据表上并将结果绑定到另一个数据表中 [英] How to put condition over a datatable and bind the result into another datatable

查看:75
本文介绍了如何将条件放在数据表上并将结果绑定到另一个数据表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存在两个数据表,现在基于我想从两个创建一个新数据表的条件。以下是两张桌子



There is two datatables are exists, now basis on a condition I want to create one new datatable from the two. Here are the two tables

Tab1ID	Name	Condition
 1	A	Jan
2	B	Jan
3	C	Jan
4	D	Jan
5	E	Feb
6	F	Feb
7	G	Feb
8	H	Mar
9	I	Mar
10	J	Mar
11	K	Apr
12	L	May
13	M	June
14	N	July
15	O	July




Tab2ID	Tab1ID	Details
1	1	AAA
2	2	BBB
3	3	CCC
4	4	DDD
5	5	EEE
6	6	FFF
7	7	GGG
8	8	HHH
9	1	ABAB
10	2	BCBC
11	3	CDCD
12	4	DEDE
13	14	NNN
14	15	OOO
15	5	EFEF
16	6	FGFG
17	7	GHGH
18	8	HIHI





现在从Tab2我得到上面的输出,

现在我在Tab1上放了一个条件即条件==Jan

得到以下结果





Now From Tab2 I’m getting the above output,
now I put one condition on Tab1 ie Condition == "Jan"
And getting the following result

Tab1ID	Name	Condition
 1	A	Jan
 2	B	Jan
3	C	Jan
4	D	Jan



基于上面的Tab1数据表我希望得到以下结果


And based on the above Tab1 datatable i want the following result

Tab2ID	Tab1ID	Details
1	1	AAA
2	2	BBB
3	3	CCC
4	4	DDD
9	1	ABAB
10	2	BCBC
11	3	CDCD
12	4	DEDE

推荐答案

试试这个:

Try this:
SELECT t2.*
FROM Table1 AS t1 INEER JOIN Table2 AS t2 ON t1.Tab1ID = t2.Tab1ID
WHERE t1.Condition = 'Jan'





关于联接的更多信息:

http://www.w3schools.com/sql/sql_join.asp [ ^ ]

SQL连接的可视化表示 [ ^ ]





如果这些是数据表,请使用选择第一个数据表以找到匹配的 Tab1Id ,然后在第二个数据表上使用相同的方法,并使用相应的 Tab1Id 。< br $> b $ b

Datatable.Select方法 [ ^ ]

[/编辑]





示例:



More about joins:
http://www.w3schools.com/sql/sql_join.asp[^]
Visual Representation of SQL Joins[^]


If those are datatables, use select on first datatable to find matching Tab1Id, then use the same method on the second datatable with the corresponding Tab1Id.

Datatable.Select method[^]
[/EDIT]


Example:

DataTable table1 = DataSet1.Tables["Table1"];
DataTable table2 = DataSet1.Tables["Table2"];
string firstcondition = "Condition='Jan'";
DataRow[] foundTab1Rows;
DataRow[] foundTab2Rows;

// Use the Select method to find all rows matching the filter.
foundTab1Rows = table1.Select(firstcondition);

// Print column 0 of each returned row.
for(int i = 0; i < foundTab1Rows.Length; i ++)
{
    string secondcondition = "TablId=" + foundTab1Rows[i][colIndexForTab1Id];
    foundTab2Rows = table2.Select(secondcondition);
    for(int j = 0; j < foundTab2Rows.Length; j ++)
    {
        Console.WriteLine(foundTab2Rows[j][0]);
    }
}



[/ EDIT]


[/EDIT]


这篇关于如何将条件放在数据表上并将结果绑定到另一个数据表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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