c# - Linq Outer join Object reference not set to an instance of an object DefaultIfEmpty -


i trying produce list of units required carry out audit date of audit if been carried out , if no audit leave if blank.

i have following in method:

public actionresult auditreportlist(int stnassureauditid)     {         //get data list         var people = new list<people.models.person>(peopledb.people);         var reports = new list<stnassureauditreport>(db.stnassureauditreports);         var units = new list<people.models.unit>(peopledb.units);         var auditunits = new list<stnassureunit>(db.stnassureunits).where(x => x.stnassureauditid == stnassureauditid);          var auditreportlist = u in auditunits                               join r in reports on u.unitid equals r.unitid ur                               in ur.defaultifempty()                               select new                               {                                   carriedout = (a == null ? string.empty : a.carriedout.tolongdatestring()),                                   stnassureauditreportid = (a == null ? 0 : a.stnassureauditreportid),                                   unitid = a.unitid,                                   complete = (a == null ? false : true),                                   stncommid = a.stncommid,                                   watchcommid = a.watchcommid                               };          var auditunitslist = u in auditreportlist                              join r in units on u.unitid equals r.unitid                              select new                               {                                  unitid = u.unitid,                                  unitname = r.unitname,                                  carriedout = u.carriedout,                                  stnassureauditreportid = u.stnassureauditreportid,                                  complete = u.complete,                                  stncommid = u.stncommid,                                  watchcommid = u.watchcommid                              };          var reportstncomm = (from c in auditunitslist                              join d in people on c.stncommid equals d.personid                              select new reportstncomm                              {                                  stnassureauditreportid = c.stnassureauditreportid,                                  stncomm = d.firstname + " " + d.lastname,                                  watchcommid = c.watchcommid,                                  unitname = c.unitname,                                  carriedout = c.carriedout,                                  unitid = c.unitid,                                  complete = c.complete,                              }).tolist();          var reportlist = (from h in reportstncomm                           join f in people on h.watchcommid equals f.personid                           select new stnassurereportlist                           {                               carriedout = h.carriedout,                               stnassureauditreportid = h.stnassureauditreportid,                               stncomm = h.stncomm,                               unitname = h.unitname,                               watchcomm = f.firstname + " " + f.lastname,                               unitid = h.unitid,                               complete = h.complete,                           }).orderby(x => x.unitname).tolist();           var viewmodel = reportlist.select(t => new auditreportlistviewmodel         {             carriedout = t.carriedout,             stnassureauditreportid = t.stnassureauditreportid,             stncomm = t.stncomm,             unitname = t.unitname,             watchcomm = t.watchcomm,             complete = t.complete         });          return view("auditreportlist", viewmodel);     } 

however when run

object reference not set instance of object.

on auditreportlist variable.

looking @ debug, appear null after 3 loops through, presumed in case defaultifempty kick in == null can sused in select doesnt seems working

guarding against null (ternaries a == null ? ... : ...) on cases while forgetting others mistake. should guard on cases. here, not done on unitid, stncommid , watchcommid.

side note: in team have asked banning linq sql syntax. looks me 'false idea'. appealing write sql compiled queries in .net, in practice found them far less readable , understandable linq lambda based extensions (as where(a => ...), select(a => ...), ...).


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -