C# linq中可为空字段的和
我有一个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
(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似乎是可空的
不是集合或列表