Linq connection request error

I am trying to join multiple datasets to create a single datatable type. Here is the request.

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 and r0w4 as LEFT OUTER JOIN. But here I get an error

Object reference not set to object instance

The error seems to be in

r0w4.ItemArray

Maybe r0w4 does not get any value. What could be the possible reason?

+4
source share
1 answer

The task ps.DefaultIfEmpty()will return the default value ( nullin this case) if no row matches and thus throws this error.

You can change it like this: -

r0w3.ItemArray.Concat(r0w4 != null ? r0w4 .ItemArray : new object[] {}))
+7
source

Source: https://habr.com/ru/post/1619151/


All Articles