Tom*_*len 8 c# linq asp.net sum
我有查询:
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");
}
Run Code Online (Sandbox Code Playgroud)
当我aggPlays
在上面的循环中打印出来时,它们会出现:
{21}
{}
{}
{}
Run Code Online (Sandbox Code Playgroud)
问题似乎是如果没有记录则Sum()
返回null
.我不知道如何解决这个问题,以至于不c.Plays + null
等于null
而且只是c.Plays
.
Ree*_*sey 17
你可以通过不返回来纠正这个问题int?
,而是int
直接转换为:
.Sum(v => v.Plays ?? 0)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
14962 次 |
最近记录: |