Tom*_*len 13 linq math sum set linq-to-sql
如果我做:
int updateGamePlays = db.tblArcadeGames.Where(c => c.ParentGameID == GameID).Sum(c => c.Plays);
Run Code Online (Sandbox Code Playgroud)
如果此查询中未返回任何记录,则抛出:
System.InvalidOperationException:无法将null值分配给类型为System.Int32的成员,该成员是非可空值类型.
让它返回0的唯一方法是:
int updateGamePlays = db.tblArcadeGames.Where(c => c.ParentGameID == GameID).Sum(c => (int?)c.Plays) ?? 0;
Run Code Online (Sandbox Code Playgroud)
在数据库中c.Plays是一个不可为空的int.
在集合论中,空集的总和应该等于0(ref).Linq-to-SQL是如何决定让它返回的null呢?