Linq query with nullable sum

前端 未结 14 1905
粉色の甜心
粉色の甜心 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:57

    I had a similar issue and came up with the solution of getting whatever I was trying to get out of the database, do a count on those and then only if I had anything returned do a sum. Wasn't able to get the cast working for some reason so posting this if anyone else had similar issues.

    e.g.

    Votes = (from v in Db.Votes
              where b.ItemId = v.ItemId
              select v)
    

    And then check to see if you've got any results so that you don't get null returned.

    If (Votes.Count > 0) Then
        Points = Votes.Sum(Function(v) v.Points)
    End If
    

提交回复
热议问题