Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# linq中可为空字段的和_C#_Sql_Linq - Fatal编程技术网

C# linq中可为空字段的和

C# linq中可为空字段的和,c#,sql,linq,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.Branch

我有一个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);

我想求和hoghughlist.NGPTotalDaramadeMashmol并按其他字段分组,但hoghughlist.NGPTotalDaramadeMashmol没有求和函数,因为它的长度可以为null。这是一种求和此值并按其他字段分组的方法吗?

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

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

Sum
将使用一组数字,而不是单个数字。我不确定您试图实现的是什么。ngptotataldaramademashmol似乎是
可空的
不是集合或
列表