I am trying to create an 'if' statement inside the coding below:
var qisg = new QuoteItemSectionGroup
{
SectionGroup = db.SectionGroups.Where(x => x.Name == "Longitudinals" && x.Section == TruckSection.Floor).First(),
StockItem = quoteItem.Chassis.Longitudinal, <<-- Here
Quantity = 2,
Length = globals.FloorCalculatedLength
};
Example:
if (quoteItem.Chassis.Longitudinal == "SCH100")
Stockitem = quoteItem.BodyType.Longitudinal;
Is there a way that I might be able to create a method like this in my var qisg?
EDIT: This is what the code looks like now
var qisg = new QuoteItemSectionGroup
{
SectionGroup = db.SectionGroups.Where(x => x.Name == "Longitudinals" && x.Section == TruckSection.Floor).First(),
StockItem = quoteItem.BodyType.Longitudinal == "SCH100" ? quoteItem.Chassis.Longitudinal : quoteItem.BodyType.Longitudinal,
Quantity = 2,
Length = globals.FloorCalculatedLength
};
I'm getting the error:
Operator '==' cannot be applied to operands of type 'TrucksWcf.Models.StockItem' and 'string'
I'm sorry but some of the answers are a bit too complex for me to understand 0_o
ALSO Here is an example of another StockItem being assigned to a product:
qisg = new QuoteItemSectionGroup
{
SectionGroup = db.SectionGroups.Where(x => x.Name == "Cross Member" && x.Section == TruckSection.Floor).First(),
StockItem = db.StockItems.Where(x => x.StockCode == "SCH075").First(),
Length = globals.FloorCalculatedWidth
};
As others have said, the conditional operator is perfect if it's a simple if this..then that... otherwise something else scenario.
If your conditions are more complex you can create a method which checks the condition and returns the appropriate value. So something like...
Edit: I've just seen your update with the error message. It looks like
quoteItem.BodyType.Longitudinal
is of typeStockItem
. Given your last code snippet shows that aStockItem
has aStockCode
I think you probably need something like this...