I would like to use the if statement below to calculate a StockCode for my product, but I am getting the error:
Cannot implicitly convert type 'TruckWcf.Models.StockItem' to 'bool'
Now I am a newbie in C# as well as EF6, so I am trying my best to understand what is going on here :P.
var qisg = new QuoteItemSectionGroup
{
SectionGroup = db.SectionGroups.Where(x => x.Name == "Longitudinals" && x.Section == TruckSection.Floor).First(),
StockItem = db.StockItems.Where(x => x.StockCode == "SCH113").First() ? quoteItem.Chassis.Longitudinal : quoteItem.BodyType.Longitudinal, // <<-- Here lies my error
Quantity = 2,
Length = globals.FloorCalculatedLength
};
Can someone please advise me how to fix this small, yet simple problem. Thank you!
Just use the
??
operator to choose the value. That essentially say take the first value before the??
but if it's null, return the value after. Also useFirstOrDefault
or you may get an exception rather than anull
return value. Finally removingWhere
as it's simpler to write this way (good spot by @YuvalItzchakov):