Note that there are some explanatory texts on larger screens.

plurals
  1. POMerging two datatable in memory and grouping them to get sum of columns.Using linq but kind of lost here
    primarykey
    data
    text
    <p>I have two table where two column are fixed. Some columns are identical and some are new.<strong>Columns are dynamic.</strong></p> <p><strong>Have to do it in code level and I am trying to loop and conditions</strong></p> <p>What I want is to generate a report following the condition,</p> <ul> <li>All columns in table1 and table2 must be present.</li> <li>If a column is common and value is there it should be added with the identical row in other table.</li> <li>If any row is present in one table but not in other, it should be included.</li> </ul> <p><strong>Example data</strong></p> <p>Table1</p> <pre><code>ID | NAME | P1 | P2 | P3 ---------------------------- 1 | A1 | 1 | 2 | 3.3 2 | A2 | 4.4 | 5 | 6 </code></pre> <p>TABLE 2 </p> <pre><code> ID | NAME | P1 | P2 | P4 --------------------------- 1 | A1 | 10 | 11 | 12 2 | A2 | 12 | 14 | 15 3 | A3 | 16 | 17 | 18 </code></pre> <p>Expected output:</p> <pre><code> ID | NAME | P1 | P2 | P3 | P4 --------------------------------- 1 | A1 | 11 | 13 | 3.3 | 12 2 | A2 | 16.4 | 19 | 6 | 15 3 | A3 | 16 | 17 | null| 18 </code></pre> <p>Progress till now:</p> <p>First I merged those two table in to table1 <code>table1.Merge(table2)</code></p> <p>Then trying to group by over it</p> <pre><code>var query = from row in table1.AsEnumerable() group row by new { ID = row.Field&lt;int&gt;("ID"), Name = row.Field&lt;string&gt;("Name") } into grp select new { ID = grp.Key.ID, Name = grp.Key.Name, Phase1 = grp.Sum(r =&gt; r.Field&lt;decimal&gt;("P1")) }; </code></pre> <p>I have modified this code to get a datatable. <strong>Please see attached cs file.</strong><br> This is working, but as the number of columns are dynamic, I guess I have to repeat it for other columns and join all these small tables where one columns will be added.</p> <p>How can I merge all those small tables?</p> <p>I am lost here.Is there any other way. Its feeling as stupid thing.</p> <p>Any help would be appreciated.</p> <p>Attached File:</p> <hr> <p><a href="http://dl.dropbox.com/u/26252340/Program.cs" rel="nofollow">http://dl.dropbox.com/u/26252340/Program.cs</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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