while i know i can make a join of 2 rows via sql, my program doesn't use it i have 2 datatables and i take each row, compare to the rows on the other table and want to make a join out of it
public DataTable joinTables (DataTable t1, DataTable t2)
{
DataTable joinTable = new DataTable();
foreach (DataRow r1 in t1.Rows)
{
foreach (DataRow r2 in t2.Rows)
{
///if (....)
joinTable.ImportRow(joinRows(r1,r2));
}
}
return joinTable;
}
public DataRow joinRows (DataRow r1, DataRow r2)
{
DataRow joinRow = new DataRow();
///....
return joinRow;
}
I think you may have vastly underestimated the complexity of what you're looking for, but here is some code that will do it, but it has some major assumptions I'll discuss.
Assumptions
DataTable
has the same number of rows.DataTable
objects are sorted in the order you want them merged by index.These assumptions are major. In short, though this produces the desired outcome, I'm unsure it's really what you're looking for, and I'm unsure you're really sure what you're looking for.
Alright all you mad lads out there, I think I've cracked it.
This is what I came up with for my problem, a bit of a pain in arse way of doing things, but it got the two datarows as one which is what I wanted.
Could not find anything as a default that did this, but please do let me know if it exists.
An instance of how you could use it is perhaps using LINQ to join two separate data tables together and you want the rows to act accordingly, see example below:
Here's an example of two ways to do join using LINQ.