1. 程式人生 > >Nullable object must have a value

Nullable object must have a value

ble fault 就會 nullable 一個 where empty 有一個 inner

有一個linq查詢,由inner join改成left join, 對於有空值,就會出現Nullable object must have a value 的錯誤.

原來的查詢:

 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 }

Nullable object must have a value