Linq query with nullable sum

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

    Just to add another method into the mix :)

    Where(q=> q.ItemId == b.ItemId && b.Points.HasValue).Sum(q=>q.Points.Value)
    

    I had a similar scenario but I wasn't comparing an additional field when summing...

    Where(q => q.FinalValue.HasValue).Sum(q=>q.FinalValue.Value);
    

提交回复
热议问题