Left outer join null using VB.NET and LINQ

I have what I consider a working left LINQ outer join, but I am having problems with the selection due to a null value on the right side of the join. That's what i still have

Dim Os = From e In oExcel Group Join c In oClassIndexS On c.tClassCode Equals Mid(e.ClassCode, 1, 4) Into right1 = Group _ From c In right1.DefaultIfEmpty 

I want to return all e and one column from c called tClassCode . I was wondering what would be the syntax. As you can see, I am using VB.NET .

Updating ...

Here is the request that makes the connection, where I get the error:

_message = "The reference to the object is not installed in the instance of the object."

  Dim Os = From e In oExcel Group Join c In oClassIndexS On c.tClassCode Equals Mid(e.ClassCode, 1, 4) Into right1 = Group _ From c In right1.DefaultIfEmpty Select e, c.tClassCode 

If I remove c.tClassCode from select, the request will be executed without errors. So I thought that maybe I need to make a new one, but I don’t think that I do it either.

+4
source share
2 answers

Ok, I got his job ... At least I get the results without errors. Thanks for the help ... Here it is.

  Dim Os = From e In oExcel Group Join c In oClassIndexS On c.tClassCode Equals Mid(e.ClassCode, 1, 4) Into right1 = Group _ From jo In right1.DefaultIfEmpty() Select New With {.CivilServiceTitle = e.CivilServiceTitle, .Code = If(jo Is Nothing, Nothing, jo.tClassCode)} 
+1
source

EDIT: you need to check c for null, namely c after your grouping. See my updates below.

You need to do tClassCode c null check in your select statement. What type of tClassCode ? You should be able to perform a null check on the value of c , and if it is null, assign a NULL value of the appropriate type and return it, otherwise return the actual value.

Since I’m not sure if tClassCode is tClassCode be an integer, in this case the cast will have an integer with a null value ( Integer? ). With that in mind, your select statement, which will be added at the end of what you still have, should resemble:

Since tClassCode is a line that will look like your code:

 Select _ e, _ Code = If(c Is Nothing, Nothing, c.tClassCode) 

Depending on what you need to do, if c is null, you can return String.Empty instead of Nothing :

 Select _ e, _ Code = If(c Is Nothing, String.Empty, c.tClassCode) 

Of course, you can further expand the "e" selection to design its specific columns by name.

It is important to understand that you must check c for null before using any of its properties, since it can be zero depending on the result of an external outer join for a particular result (string). Returning to my earlier example, if you have another field named Priority that was an integer that you would choose against nullable:

 Select _ e, _ Priority = If(c Is Nothing, CType(Nothing, Integer?), c.Priority) 
+3
source

All Articles