How to compare null in linux syntax of connection

Work on EF 4 C #. The challenge is to connect. sql syntax

Select a.Code, b.Name from DepartmentMaster a Join DepartmentDetail b on isnull( a.ID,0) =isnull( b.ID,0) 

Note: a.ID, b.ID Both values ​​are null

Want the syntax above the Linq syntax. The syntax syntax is not working for me

 Var r=from a in DepartmentMaster Join b in DepartmentDetail on a.ID equals b.ID Select a.Code,b.Name 

Need help writing syntax sql isnull () of a comparable process in linq ef.

If you have any questions, please ask, thanks in advanced

+4
source share
4 answers

You can use the null-coalescing operator to provide a default value if id is null:

 from c in DepartmentMaster join b in DepartmentDetail on (a.ID ?? 0) equals (b.ID ?? 0) select new { a.Code, b.Name } 

This will create a request

 SELECT [t0].[Code], [t1].[Name] AS [ID1] FROM [DepartmentMaster] AS [t0] INNER JOIN [DepartmentDetail] AS [t1] ON (COALESCE([t0].[ID],@p0)) = (COALESCE([t1].[ID],@p1)) 

This is not exactly the same as ISNULL , but the result should be the same.

+2
source

it can help you

 Var r=from a in DepartmentMaster Join b in DepartmentDetail on a.(x => (int?)a.ID) ?? 0 equals b.(y => (int?)b.ID) ?? 0 Select a.Code,b.Name 
+2
source

You can use DefaultIfEmpty , it replaces an empty collection with a collection of one default value. since the default value for int is 0

 var r=from a in DepartmentMaster join b in DepartmentDetail on a.ID.DefaultIfEmpty() equals b.ID.DefaultIfEmpty() Select a.Code,b.Name 
+1
source

You should do something like this:

 Join b in DepartmentDetail on (a.ID == null ? 0 : a.ID) equals (b.ID == null ? 0 : b.ID) 
0
source

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


All Articles