SORU
30 Mart 2009, PAZARTESİ


Null toplamı ile seri sorgusu

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()
}

Bu sorgu var, ancak eğer hayır oyları dışında bulunursa başarısız:

The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.

Çünkü toplamını verir bir int ve null bir int topla (int vererek değil sanırım? giriş sadece aynı hatayı verdiği gibi, muhtemelen in üzerinde sadece çalışır sum neden.

Bunun için iyi bir çözüm?

CEVAP
9 ŞUBAT 2010, Salı


Toplamı null formunu kullanarak, bir null değerini döküm deneyin.

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)
}

Sorununuzu daha ayrıntılı olarak burada ele alınmıştır:

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

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Easy Learn Tutorial

    Easy Learn T

    10 Kasım 2012
  • PhoneBuff

    PhoneBuff

    10 HAZİRAN 2011
  • WiseOwlTutorials

    WiseOwlTutor

    21 EKİM 2011