Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to use an implementation of a full outer join. Something like what follows.</p> <p>Some setup so you can try this yourself:</p> <pre><code>DataTable t1 = new DataTable(); t1.Columns.Add("ID", typeof(int)); t1.Columns.Add("Name", typeof(string)); t1.Columns.Add("P1", typeof(double)); t1.Columns.Add("P2", typeof(double)); t1.Columns.Add("P3", typeof(double)); DataRow dr1 = t1.NewRow(); dr1["ID"] = 1; dr1["Name"] = "A1"; dr1["P1"] = 1; dr1["P2"] = 2; dr1["P3"] = 3.3; t1.Rows.Add(dr1); DataRow dr2 = t1.NewRow(); dr2["ID"] = 2; dr2["Name"] = "A2"; dr2["P1"] = 4.4; dr2["P2"] = 5; dr2["P3"] = 6; t1.Rows.Add(dr2); DataTable t2 = new DataTable(); t2.Columns.Add("ID", typeof(int)); t2.Columns.Add("Name", typeof(string)); t2.Columns.Add("P1", typeof(double)); t2.Columns.Add("P2", typeof(double)); t2.Columns.Add("P4", typeof(double)); DataRow dr3 = t2.NewRow(); dr3["ID"] = 1; dr3["Name"] = "A1"; dr3["P1"] = 10; dr3["P2"] = 11; dr3["P4"] = 12; t2.Rows.Add(dr3); DataRow dr4 = t2.NewRow(); dr4["ID"] = 2; dr4["Name"] = "A2"; dr4["P1"] = 12; dr4["P2"] = 14; dr4["P4"] = 15; t2.Rows.Add(dr4); DataRow dr5 = t2.NewRow(); dr5["ID"] = 3; dr5["Name"] = "A3"; dr5["P1"] = 16; dr5["P2"] = 17; dr5["P4"] = 18; t2.Rows.Add(dr5); </code></pre> <p>The queries look like:</p> <pre><code>var ids = (from r1 in t1.AsEnumerable() select new { ID = r1["ID"], Name = r1["Name"] }).Union( from r2 in t2.AsEnumerable() select new { ID = r2["ID"], Name = r2["Name"] }); var query = from id in ids join r1 in t1.AsEnumerable() on id equals new { ID = r1["ID"], Name = r1["Name"] } into left from r1 in left.DefaultIfEmpty() join r2 in t2.AsEnumerable() on id equals new { ID = r2["ID"], Name = r2["Name"] } into right from r2 in right.DefaultIfEmpty() select new { ID = (r1 == null) ? r2["ID"] : r1["ID"], Name = (r1 == null) ? r2["Name"] : r1["Name"], P1 = (r1 == null) ? r2["P1"] : (r2["P1"] == null) ? r1["P1"] : (double)r1["P1"] + (double)r2["P1"], P2 = (r1 == null) ? r2["P2"] : (r2["P2"] == null) ? r1["P2"] : (double)r1["P2"] + (double)r2["P2"], P3 = (r1 == null) ? null : r1["P3"], P4 = (r2 == null) ? null : r2["P4"] }; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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