Note that there are some explanatory texts on larger screens.

plurals
  1. PODataTable Linq join many columns
    text
    copied!<p>I have a trouble with Linq Join. I want to join 2 tables, which they have same structure with n-columns. My problem is i don't know the names of those columns, so how can i rewrite those in select new?</p> <p>Table 1: Here I have some parameters in ID, Name and LastName. Comment, Attribute and the rest are null</p> <pre><code> ID Name LastName Comment Attribute ... "what" "ABC" ... "hello" "SDE" ... 3 lola 1 de 4 miki ... ... ... ... </code></pre> <p>Table 2: Here is the same like Table 1 but it has some parameters in Comment, Attribute and the Rest. </p> <pre><code> ID Name LastName Comment Attribute ... "what" "ABC" ... "hello" "SDE" ... 1 de "hi" 4 miki "OKK" 3 lola "yo" "LL" </code></pre> <p>Result: I would like to have joined Table like this</p> <pre><code> ID Name LastName Comment Attribute ... "what" "ABC" ... "hello" "SDE" ... 3 lola "yo" "LL" 1 de "hi" 4 miki "OKK" ... ... ... ... ... ... </code></pre> <p>My Code would be:</p> <pre><code>var Result= from tb1 in table1.AsEnumerable() join tb2 in tabl2.AsEnumerable() on new { Name = tb1.Field&lt;String&gt;("Name"), LastName = tb1.Field&lt;String&gt;("LastName"), } equals new { Name=tb2.Field&lt;String&gt;("Name"), LastName=tb2.Field&lt;String&gt;("LastName"), } into grp1 from tb3 in grp1.DefaultIfEmpty() select new { ID = tb1.Field&lt;String&gt;("ID"), Name = tb1.Field&lt;String&gt;("Name") , LastName = tb1.Field&lt;String&gt;("LastName"), Comment = tb3!= null ? tb3.Field&lt;String&gt;("Comment") : null, Attribute= tb3!= null ? tb3.Field&lt;String&gt;("Attribute") : null, ... // Here should be next Columns Name but don't know how to put there }; </code></pre> <hr> <p>I tried with this code but my compiler just hanged out, dont know why</p> <pre><code> for (int i = 2; i &lt; table1.Rows.Count; i++) { foreach (DataRow dr in table2.Rows) { if ((table1.Rows[i]["Name"].ToString() == dr["Name"].ToString())&amp;&amp;table1.Rows[i]["LastName"].ToString() == dr["LastName"].ToString()) { table1.Rows.RemoveAt(i); table1.ImportRow(dr); } } } dataGridView1.DataSource = table1; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload