Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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


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

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

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
Posted
Updated 12-May-13 20:00pm
v3

1 solution

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


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

[EDIT #1]
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]

[EDIT #2]
Example:
C#
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]
 
Share this answer
 
v3
Comments
sahabiswarup 13-May-13 2:02am    
as i already said that those are not a table, those are datatable. so please please tell me how to do the same thing for a datatable?
Maciej Los 13-May-13 2:10am    
Please, see my updated answer.
sahabiswarup 13-May-13 2:14am    
Can you please give me one example so that it'll easy for me to understand.
Thanks in advance.
Maciej Los 13-May-13 2:23am    
Please, follow the link, there you'll find an example.
http://msdn.microsoft.com/en-us/library/det4aw50.aspx[^]
sahabiswarup 13-May-13 2:19am    
Content has been removed, because it was a copy of question.
Maciej Los

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900