Hi,
I have two tables :
Category(categ)
Sub Category (scat)
I want to perform the following query on the above tables.
select a.code, a.name, a.type, b.category as category from categ a left join scat b on a.code=b.code
The above sql when executed on SQL Server this is the result.
code name type category
1001 abc F NULL
1002 def F NULL
1003 ghi B NULL
I tried this LINQ for the above query :
Please note : The scats context table is an empty table.
List<categscat> lstcateg = (from categrow in Context.categs.AsEnumerable()
join scatrow in Context.scats
on categrow.code equals scatrow.code
select new categscat(categrow.code, categrow.name,
categrow.type, categrow.cate,
joinedrow.category)).ToList();
This returns a NullReferenceException.
What I have tried:
tried using the DefaultIfEmpty() as per suggestions in the link below :
https://www.codeproject.com/articles/169590/linq-to-sql-left-join-with-null-values
lstcateg = (from categrow in dlsmain.odlsContext.categs.AsEnumerable()
join scatrow in dlsmain.odlsContext.scats.AsEnumerable()
on categrow.code equals scatrow.code into jointable
from joinedrow in jointable.DefaultIfEmpty()
select new categscat(categrow.code, categrow.name, categrow.type, categrow.cate, joinedrow.category)).ToList();
Still I'm getting the same error. What is my mistake? How to get this right ? Please suggest.