I am trying to join multiple datatables to create a single datatable. Here is the query.
var row = from r0w1 in dt_vi.AsEnumerable()
join r0w2 in dt_workcenter.AsEnumerable()
on r0w1.Field<int>("wcID") equals r0w2.Field<int>("iD")
join r0w3 in dt_recipe.AsEnumerable()
on r0w1.Field<int?>("curingRecipeID") equals r0w3.Field<int?>("recipe_id") join r0w4 in dt_defect.AsEnumerable()
on r0w1.Field<int?>("defectID") equals r0w4.Field<int?>("defect_id") into ps
from r0w4 in ps.DefaultIfEmpty()
select r0w1.ItemArray.Concat(r0w2.ItemArray.Concat(r0w3.ItemArray.Concat(r0w4.ItemArray))).ToArray();
foreach (object[] values in row)
dt.Rows.Add(values);
I tried to join r0w1 & r0w4 as LEFT OUTER JOIN
. But here I am getting the error
Object reference not set to an instance of an object
Error seems to be in
r0w4.ItemArray
May be r0w4 is not getting any value. What could be the possible reason ?
The problem is ps.DefaultIfEmpty()
will return the default value (null
in this case) when no row match and thus it is throwing that error.
You can change it like this:-
r0w3.ItemArray.Concat(r0w4 != null ? r0w4 .ItemArray : new object[] {}))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With