linq中可空字段的总和

时间:2014-06-24 15:02:32

标签: c# sql linq

我有这个linq查询

(from diference in General.db.PTStbDifferenceByMonths 
 join list in General.db.PTStbLists on new
  {
     month = diference.ListMonth,
     year = diference.ActiveYear,
     national = diference.NationalCode,
     shobe = diference.BranchCode
  }
  equals new
  {
     month = list.Month,
     year = list.Year,
     national = list.NationalCode,
     shobe = list.ShobeCode
  }
  join hoghoughList in General.db.PTStbhogogMomayezs on new
  {
     melliCode = diference.MelliCode,
     national = diference.NationalCode,
     shobe = diference.BranchCode,
     serial = list.Id
  }
  equals new
  {
     melliCode = hoghoughList.MelliCode,
     national = hoghoughList.NationalCode,
     shobe = hoghoughList.ShobeCode,
     serial = hoghoughList.SerialList
  }
  join karmand in General.db.PTStbKarkonans on 
  hoghoughList.MelliCode equals karmand.MelliCode
  where diference.ListMonth == activeMonth && diference.ActiveYear == activeYear && 
  diference.NationalCode == id && diference.BranchCode == branchCode
  select new ReturnedDifference
  {
     Maliat = hoghoughList.FinalTax,
     ComputedMaliat = diference.ComputedMaliat,
     ComputedMoghayerat = diference.ComputedMoghayerat,
     hoghough = (hoghoughList.NGPTotalDaramadeMashmol ?? 0),
     ListId = diference.ListMonth,
     MelliCode = karmand.MelliCode,
     LastName = karmand.Family,
     Name = karmand.Name,
     PayedTax = diference.PayedTax,
     ActualPayedTax = hoghoughList.FinalTax
  }).OrderBy(l => l.MelliCode);

我想总结 hoghoughList.NGPTotalDaramadeMashmol 并按其他字段分组,但是hoghoughList.NGPTotalDaramadeMashmol没有Sum函数,因为它可以为空。它是一种通过其他字段对这个值和组进行求和的方法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用合并??将可为空的nullable视为0:

.Sum(v => (v.hoghoughList.NGPTotalDaramadeMashmol ?? 0))