有一个linq查询,由inner join改成left join, 对于有空值,就会出现Nullable object must have a value 的错误.

原来的查询:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
 var qry =
            from c in _context.CCC
            join f in _context.FFF .Where(t=>t.IsActive==true)
            on  new { c.ProjectId, cat = c.Category } equals new { f.ProjectId, cat = f.Category }
            where  c.IsActive == true 
            select
            new
            {
                c.Id,
                f.category
         }

 

left join, select的字段做null判断

 var qry =
            from c in _context.CCC
            join f in _context.FFF .Where(t=>t.IsActive==true)
            on  new { c.ProjectId, cat = c.Category } equals new { f.ProjectId, cat = f.Category }
            into temp from tt in temp.DefaultIfEmpty()
            where  c.IsActive == true 
            select
            new
            {
                c.Id,
                category=(f==null)?'':f.category
         }

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄