我在Db中有一个类型为decimal
的列,它也可以为空。我想要的是,如果它为null,则显示0.0
。为此,我正在尝试遵循我的LINQ声明:
CostPerUnit = od.CostPerUnit == null ? "0.0": od.CostPerUnit
哪个出错:
Error 9 Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'decimal'
也为ref提供了完整声明:
var rmas = (
from o in db.Orders
join od in db.OrderDetails on o.OrderNumber equals od.OrderNumber
join r in db.RMAs on o.OrderNumber equals r.OrderNumber
join rd in db.RMADetails on r.RMANumber equals rd.RMANumber
where (rd.WrongSKU == od.SKU)
join i in db.Inventory on od.SKU equals i.LocalSKU into grp
from g in grp.DefaultIfEmpty()
select new
{
r.RMANumber
,
r.Reason
,
o.Name
,
o.Company
,
o.Address
,
o.Address2
,
o.City,
o.State
,
o.Country
,
o.Email
,
o.Zip
,
o.Phone
,
o.ShipName,
o.ShipCompany
,
o.ShipAddress
,
o.ShipAddress2
,
o.ShipCity
,
o.ShipCountry
,
o.ShipState
,
o.ShipPhone
,
o.ShipZip
,
o.OrderNumber
,
o.ShippingTotal
,
o.BalanceDue
,
r.Status
,
OrderDate = o.OrderDate
,
DateIssued = r.DateIssued
,
SerialNumbers = rd.SerialNumbers ?? ""
,
o.SourceOrderID
,
od.SKU
,
od.ItemNumber
,
QTYOrdered = od.QuantityOrdered
,
od.QuantityReturned
,
rd.QuantityAuthorized
,
RMA_ItemNumber1 = rd.ItemNumber
,
rd.Price
,
rd.WrongSKU
,
g.Text1
,
CostPerUnit = od.CostPerUnit == null ? "0.0": od.CostPerUnit
}
).Take(50).ToList();
答案 0 :(得分:3)
你应该做的是
CostPerUnit = od.CostPerUnit ?? 0.0m
这使用Null Coalescing运算符。它专门用于为可空类型添加默认值
您可以做的是
CostPerUnit = od.CostPerUnit == null ? 0.0m : od.CostPerUnit.Value
注意.Value
属性。它得到了十进制的非可空版本。
您可能打算做的是
CostPerUnit = od.CostPerUnit == null ? "0.0": od.CostPerUnit.Value.ToString()
这将使CostPerUnit
成为一个字符串