I'm having problems with Linq Join. I want to join two tables that have the same structure with n-columns. My problem is that I do not know the names of these columns, so how can I rewrite them in select new?
Table 1: Here I have some parameters in ID, Name and LastName. Comment, Attribute, and the rest are null
ID Name LastName Comment Attribute ... "what" "ABC" ... "hello" "SDE" ... 3 lola 1 de 4 miki ... ... ... ...
Table 2: This is the same as in Table 1, but there are some parameters in the Comment, Attribute, and Rest parameters.
ID Name LastName Comment Attribute ... "what" "ABC" ... "hello" "SDE" ... 1 de "hi" 4 miki "OKK" 3 lola "yo" "LL"
Result: I would like to join a table like this
ID Name LastName Comment Attribute ... "what" "ABC" ... "hello" "SDE" ... 3 lola "yo" "LL" 1 de "hi" 4 miki "OKK" ... ... ... ... ... ...
My code will be:
var Result= from tb1 in table1.AsEnumerable() join tb2 in tabl2.AsEnumerable() on new { Name = tb1.Field<String>("Name"), LastName = tb1.Field<String>("LastName"), } equals new { Name=tb2.Field<String>("Name"), LastName=tb2.Field<String>("LastName"), } into grp1 from tb3 in grp1.DefaultIfEmpty() select new { ID = tb1.Field<String>("ID"), Name = tb1.Field<String>("Name") , LastName = tb1.Field<String>("LastName"), Comment = tb3!= null ? tb3.Field<String>("Comment") : null, Attribute= tb3!= null ? tb3.Field<String>("Attribute") : null, ...
I tried with this code, but my compiler just hanged myself, I don't know why
for (int i = 2; i < table1.Rows.Count; i++) { foreach (DataRow dr in table2.Rows) { if ((table1.Rows[i]["Name"].ToString() == dr["Name"].ToString())&&table1.Rows[i]["LastName"].ToString() == dr["LastName"].ToString()) { table1.Rows.RemoveAt(i); table1.ImportRow(dr); } } } dataGridView1.DataSource = table1;