Sum of Linq and null

I have a request:

var qq = (from c in db.tblArcadeGames where c.IsDeleted == false && c.ParentGameID == 0 && c.Approved == true let aggPlays = c.Plays + db.tblArcadeGames.Where(v => v.ParentGameID == c.ID).Sum(v => (int?)v.Plays) orderby aggPlays descending select new { c, aggPlays }) .Skip(Skip) .Take(Fetch); foreach (var g in qq) { HttpContext.Current.Response.Write("{" + g.aggPlays + "}\n"); } 

When I type aggPlays in the loop above, they exit like:

 {21} {} {} {} 

The problem is that Sum() returns null if there are no records. I'm not sure how to get around this so c.Plays + null not null , but just c.Plays .

+7
source share
2 answers

Can you fix this without returning an int? , but rather convert to int directly:

 .Sum(v => v.Plays ?? 0) 
+16
source
  int response = (from p in data.tbHoraires where p.eid == eid && p.annee == annee && p.obligatoire == true select (int?)p.nbminute ?? 0).Sum(); 
+1
source

All Articles