【发布时间】:2013-05-25 02:31:34
【问题描述】:
我正在寻找一个查询,以根据发票 ID 返回所有付款的总和 - 但是,如果没有记录任何付款,则下面的查询将返回 null。有什么方法可以返回 0 而不是 null 吗?我试过添加?? 0结尾,但是得到消息Operator ?? cannot be applied to operands of type decimal and int
AmountAllocated 是 Decimal 类型:
public decimal AmountAllocated { get; set; }
谢谢,马克
当没有找到支付行时,Sum 会返回 null:
var invoiceVM = from i in db.Invoices
where i.UserName==userName
select new NewInvoiceViewModel
{
InvoiceId = i.InvoiceId,
SumPayments =
db.PaymentInvoices.Where(pi => pi.InvoiceId == i.InvoiceId)
.Select(pi => pi.AmountAllocated).Sum()
};
以下导致Operator ?? cannot be applied to operands of type decimal and int错误:
var invoiceVM = from i in db.Invoices
where i.UserName==userName
select new NewInvoiceViewModel
{
InvoiceId = i.InvoiceId,
SumPayments =
db.PaymentInvoices.Where(pi => pi.InvoiceId == i.InvoiceId)
.Sum(pi => pi.AmountAllocated) ?? 0
};
如果已付款,则 AmountAllocated 正确返回这些付款的总和 - 如果没有找到付款行,则返回 null。
从下面的截图可以看到,第一条记录有付款,正确显示为10(十进制)-第二条记录没有付款,显示为null(十进制)。
【问题讨论】:
-
要使用
??运算符,您的操作数必须是Nullable<T>,例如decimal?或int? -
我暂时删除了我的答案,因为感觉未知信息太多。 a) 您在哪里尝试使用
??(在Sum内部或外部)? b)AmountAllocated的类型是什么? c) 当您说“下面的查询返回 null”时,您的意思是SumPayments对于某些行为 null,还是没有结果? d)AmountAllocated是否为空? -
嗨 - 我已经更新了我的问题,以更清楚地显示我在寻找什么。谢谢,马克
-
Sum()返回一个不可为空的int(或decimal),因此它将返回一个0而不是null。 -
嗨 - 它没有 - 我已经更新了我的问题以显示屏幕截图,它返回第一条记录的值,第二条记录显示为空。谢谢,马克
标签: c# asp.net-mvc linq linq-to-sql