即使有Null Reference Exeption
代码,真的有可能在连接语句上引起DefaultIfEmpty()
吗?
var getWorkerList = (from a in workerList
join ps in db.ProjectStatus on a.StatusId equals ps.StatusId into hjps
from ps in hjps.DefaultIfEmpty()
join psc in db.StatusColor on ps.StatusCode equals psc.StatusCode into hjpsc
from psc in hjpsc.DefaultIfEmpty()
join lu in db.LOOKUPS on psc.StatusColorID equals lu.LOOKUPS_ID into hjlu
from lu in hjlu.DefaultIfEmpty()
select new WorkerModel()
{
FullName = a.FullName
,Color = lu.LOOKUPS_Code
}).OrderBy(a => a.WorkerId).ToList();
这是我遇到NullReferenceExeption
错误的地方:
join lu in db.LOOKUPS on psc.StatusColorID equals lu.LOOKUPS_ID into hjlu
from lu in hjlu.DefaultIfEmpty()
添加此连接语句时,出现错误。请帮助:(
答案 0 :(得分:4)
.DefaultIfEmpty()
返回具有单个默认值的序列,如果原始来源为空。
如果原始来源为空,它仍将引发空引用异常。
答案 1 :(得分:1)
您正在使用“左联接”,而不是“ .DefaultIfEmpty()”的“联接”
所以您必须更正选择语句:将'Color = lu.LOOKUPS_Code'替换为:'Color = lu == null? “”:lu.LOOKUPS_Code'。
您还可以使用'.DefaultIfEmpty()'的不同重载来设置替换项,而不是'null',因此select语句不会引发异常