I have a column of type decimal
in Db which is nullable too. What I want that if it's null then display 0.0
. For that I am trying following in my LINQ Statement:
CostPerUnit = od.CostPerUnit == null ? "0.0": od.CostPerUnit
Which gives error:
Error 9 Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'decimal'
Complete Statement also given for 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();
What you should do is this
CostPerUnit = od.CostPerUnit ?? 0.0m
This uses the Null Coalescing operator. It's specifically for adding default values for nullable types
What you can do is this
CostPerUnit = od.CostPerUnit == null ? 0.0m : od.CostPerUnit.Value
note the .Value
property. It get's the non-nullable version of the decimal.
what you might be intending to do is this
CostPerUnit = od.CostPerUnit == null ? "0.0": od.CostPerUnit.Value.ToString()
That will make CostPerUnit
a string
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.