Linq query with nullable sum

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

    I had the same problem. Solved it with empty list union:

    List emptyPoints = new List() { 0 };
    
    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).Union(emptyPoints).Sum()
    }
    

    In case of "Points" is integer this should work.

提交回复
热议问题