linq中可空字段的总和

本文关键字:字段 linq | 更新日期: 2023-09-27 18:10:16

我有这个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和group by由其他字段,但hoghoughList。NGPTotalDaramadeMashmol没有Sum函数因为它是长空的。它是一种方法来求和这个值和组由其他字段?

linq中可空字段的总和

您可以使用coalesce ??将可空值视为0:

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