LINQ column selection Left external join

I have three tables from a ds dataset.

var test0 = from a in ds.Tables[0].AsEnumerable()
            select a["ID"].ToString();

test0 has the following meanings:

  [0] "8" 
  [1] "9" 
  [2] "11" 
  [3] "2" 
  [4] "1"

var test1 = from a in ds.Tables[1].AsEnumerable()
            select a["SubscriptionID"].ToString();

test1 has the following meanings:

  [0] "25" 
  [1] "27" 
  [2] "4" 
  [3] "26" 
  [4] "5" 
  [5] "6" 
  [6] "1" 
  [7] "24" 
  [8] "23" 
  [9] "2" 
  [10] "9" 

var comTable1 =
  from a in ds.Tables[0].AsEnumerable()
  from b in ds.Tables[1].AsEnumerable()
    .Where(bb => bb["SubscriptionID"].ToString() == a["ID"].ToString())
    .DefaultIfEmpty()
  select b;

comTable1 returns these values ​​that are correct -

   [0] null  
   [1] {System.Data.DataRow}  
   [2] null  
   [3] {System.Data.DataRow}  
   [4] {System.Data.DataRow}

The problem for me is that if I want to select a specific field, it will throw out the message "Link to an object not installed on an instance of an object". in comTable2 with the following codes -

var comTable2 =
  from a in ds.Tables[0].AsEnumerable()
  from b in ds.Tables[1].AsEnumerable()
    .Where(bb => bb["SubscriptionID"].ToString() == a["ID"].ToString())
    .DefaultIfEmpty()
  select b["SubscriptionID"];

Why doesn't the left join in LINQ return other null values? Is there any way to avoid this?

I ask about this because my codes should continue left joining with other tables, for example -

var comTable =
  from a in ds.Tables[0].AsEnumerable()
  from b in ds.Tables[1].AsEnumerable()
    .Where(bb => bb["SubscriptionID"].ToString() == a["ID"].ToString())
    .DefaultIfEmpty()
  from c in ds.Tables[2].AsEnumerable()
    .Where(cc => cc["ID"].ToString() == (b["GroupID"]??"null").ToString())
    .DefaultIfEmpty()
  select c;

Now I could not get anything from b and c.

Thank!

+3
source share
1 answer

- , . :

var comTable2 = from a in ds.Tables[0].AsEnumerable()
                from b in ds.Tables[1]
                            .AsEnumerable()
                            .Where(bb => bb["SubscriptionID"].ToString() 
                                   == a["ID"].ToString())
                            .DefaultIfEmpty()
                select b == null ? null : b["SubscriptionID"];

, , , .

b null. , , LINQ join?

+3

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


All Articles