Click here to Skip to main content
15,909,051 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If i have a datatable that contains suppose one row e.g.

ID Name Phone
1 a 3456

and another Datatable that may contain any number of rows

ID Name Phone
2 b 1234
3 c 234
... ... ...

I wanna Create another datatable that will contain the cartesian product of the above two tables in .aspx.cs page e.g

1 2 ....
2 1 .....
1 3 ......
3 1 ......


Is it possible with Linq or some other way? Or there is any other alternative way to do It? Plz Help
Posted

1 solution

Guess this is what you want... :) Two datatables named as dt1 & dt2 & combined one as dt3

C#
var combinedRows = from a in dt1.AsEnumerable()
                   from b in dt2.AsEnumerable()
                   select new { ColumnID1 = a["ID1"], ColumnID2 = b["ID2"] };
var dt3 = new DataTable();// Also Add columns after this line, like ID1, ID2 etc...
foreach (var item in combinedRows)
{
     row = dt3.NewRow();
     row["ID1"] = item.ColumnID1;
     row["ID2"] = item.ColumnID2;
     dt3.Rows.Add(row);
}
//Now dt3 has all combinations
 
Share this answer
 
v2

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