相关疑难解决方法(0)

Linq查询具有可空和

from i in Db.Items
select new VotedItem
{
    ItemId = i.ItemId,
    Points = (from v in Db.Votes
              where b.ItemId == v.ItemId
              select v.Points).Sum()
}
Run Code Online (Sandbox Code Playgroud)

我得到了这个查询,但是如果没有找到异常的投票则它会失败:

The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.
Run Code Online (Sandbox Code Playgroud)

我假设它因为sum返回一个int而不是一个可空的int,给一个int一个?作为输入只给出相同的错误,可能导致sum仅适用于int.

有什么好的解决方法吗?

c# linq nullable sum linq-to-sql

69
推荐指数
6
解决办法
6万
查看次数

标签 统计

c# ×1

linq ×1

linq-to-sql ×1

nullable ×1

sum ×1