Note that there are some explanatory texts on larger screens.

plurals
  1. POLeft Outer Join - LINQ to Datatable
    text
    copied!<p>I'm trying to apply a left outer join using LINQ on two data tables. I'm receiving the exception listed below when I try to debug and view data contained in result variable:</p> <blockquote> <p>System.ArgumentException: Value cannot be null. Parameter name: row</p> </blockquote> <p>Code:</p> <pre><code>private DataTable DataTable1() { DataRow dataRow = null; DataTable dt1 = new DataTable(); dt1.Columns.Add("EmpId"); dt1.Columns.Add("EmpName"); dataRow = dt1.NewRow(); dataRow["EmpId"] = "EMP001"; dataRow["EmpName"] = "Ajaj Kumar"; dt1.Rows.Add(dataRow); dataRow = dt1.NewRow(); dataRow["EmpId"] = "EMP002"; dataRow["EmpName"] = "Sanjay Gupta"; dt1.Rows.Add(dataRow); dataRow = dt1.NewRow(); dataRow["EmpId"] = "EMP003"; dataRow["EmpName"] = "Ashish Charan"; dt1.Rows.Add(dataRow); dt1.AcceptChanges(); return dt1; } private DataTable DataTable2() { DataRow dr = null; DataTable dt2 = new DataTable(); dt2.Columns.Add("EmpId"); dt2.Columns.Add("Salary"); dr = dt2.NewRow(); dr["EmpId"] = "EMP001"; dr["Salary"] = "50000"; dt2.Rows.Add(dr); dr = dt2.NewRow(); dr["EmpId"] = "EMP002"; dr["Salary"] = "45000"; dt2.Rows.Add(dr); dt2.AcceptChanges(); return dt2; } </code></pre> <hr> <pre><code>private void Form1_Load(object sender, EventArgs e) { var empInfo = DataTable1().AsEnumerable(); var empSal = DataTable2().AsEnumerable(); var result = from dtEmpRow in empInfo join dtEmpSal in empSal on dtEmpRow.Field&lt;string&gt;("EmpId") equals dtEmpSal.Field&lt;string&gt;("EmpId") into outer from dtEmpSal in outer.DefaultIfEmpty() select new { Id = dtEmpRow.Field&lt;string&gt;("EmpId"), Name = dtEmpRow.Field&lt;string&gt;("EmpName"), Salary = ((dtEmpRow == null) ? "(no salary)" : dtEmpSal.Field&lt;string&gt;("Salary")) }; } </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