Linq query with nullable sum

前端 未结 14 1896
粉色の甜心
粉色の甜心 2020-11-29 06:23
from i in Db.Items
select new VotedItem
{
    ItemId = i.ItemId,
    Points = (from v in Db.Votes
              where b.ItemId == v.ItemId
              select v.Poi         


        
14条回答
  •  旧巷少年郎
    2020-11-29 06:56

    You want to use the nullable form of Sum, so try casting your value to a nullable:

    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(r => (decimal?) r.Points)
    }
    

    Your issue is discussed here in more detail:

    http://weblogs.asp.net/zeeshanhirani/archive/2008/07/15/applying-aggregates-to-empty-collections-causes-exception-in-linq-to-sql.aspx

提交回复
热议问题