我有查询:

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");
}

当我在上面的循环中打印出aggPlays时,它们显示为:
{21}
{}
{}
{}

问题似乎是,如果不存在任何记录,则Sum()返回null。我不确定如何解决这个问题,以便c.Plays + null不会等于null,而只能等于c.Plays

最佳答案

您可以通过不返回int?来更正此问题,而是直接将其转换为int:

.Sum(v => v.Plays ?? 0)

10-02 01:41