LINQ to SQL Left Join over Left Join c#

LINQ to SQL Left Join over Left Join c#

我正在尝试对现有的左联接执行左联接,因为这是一个查找 table。

类似于:

(from m in maintablerepository.AsQueryable()
join st1 in subtable1repository.AsQuerable on 
m.id = s1.id into grps1
from s in grps1.DefaultIfEmpty()
join lt1 in lookuptablerepository.AsQuerable on 
s.lkpid = lt1.id into grplt1
from lkp in grplt1.DefaultIfEmpty()

select new {

prop1 = m.prop1,
prop2 = s.prop2,
prop3 = lkp.prop3

}).ToList();

问题是,当我删除查找 table 中的左联接时,此查询运行正常,尽管子 table 中没有匹配值。

然而,将其放回原位会导致 "Object reference not set" 错误。

我什至尝试了以下方法,但还是不行:

(from m in maintablerepository.AsQueryable()
join st1 in subtable1repository.AsQuerable on 
m.id = s1.id into grps1
from s in grps1.DefaultIfEmpty()
join lt1 in lookuptablerepository.AsQuerable on 
s.lkpid = lt1.id into grplt1
from lkp in grplt1.DefaultIfEmpty()
where s!=null && lkp!=null
select new {

prop1 = m.prop1,
prop2 = (s==null)?string.Empty:s.prop2,
prop3 = (lkp==null)?string.Empty:lkp.prop3

}).ToList();

如果第一个左连接导致 null,如何在其上进行另一个连接?

问题在于 s.lkpid = lt1.id,其中 s 为空。尝试像下面那样放置 s != null

(from m in maintablerepository.AsQueryable()
join st1 in subtable1repository.AsQuerable on 
m.id = s1.id into grps1
from s in grps1.DefaultIfEmpty()
where s!=null
join lt1 in lookuptablerepository.AsQuerable on 
s.lkpid = lt1.id into grplt1
from lkp in grplt1.DefaultIfEmpty()
where lkp!=null
select new {

prop1 = m.prop1,
prop2 = (s==null)?string.Empty:s.prop2,
prop3 = (lkp==null)?string.Empty:lkp.prop3

}).ToList();

如果对您有帮助请标记post为已回答