Creating if statement inside a variable

105 views Asked by At

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
    };
4

There are 4 answers

5
amcdermott On BEST ANSWER

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...

var qisg = new QuoteItemSectionGroup
{
    SectionGroup = db.SectionGroups.Where(somecondition).First(),
    StockItem = DetermineStockItem(valueToCheck)
    Quantity = 2,
    Length = globals.FloorCalculatedLength
};


public StockItem DetermineStockItem(object param)
{
   // Include complex if and logic here.
   return SomeStockItem;
}

Edit: I've just seen your update with the error message. It looks like quoteItem.BodyType.Longitudinal is of type StockItem. Given your last code snippet shows that a StockItem has a StockCode I think you probably need something like this...

var qisg = new QuoteItemSectionGroup
{
    SectionGroup = db.SectionGroups.Where(x => x.Name == "Longitudinals" && x.Section == TruckSection.Floor).First(),
    StockItem = quoteItem.BodyType.Longitudinal.StockCode == "SCH100" ? quoteItem.Chassis.Longitudinal : quoteItem.BodyType.Longitudinal,
    Quantity = 2,
    Length = globals.FloorCalculatedLength
};
0
Dika Arta Karunia On
 var qisg = new QuoteItemSectionGroup
    {
        SectionGroup = db.SectionGroups.Where(x => x.Name == "Longitudinals" && x.Section == TruckSection.Floor).First(),
        StockItem = (quoteItem.Chassis.Longitudinal == "SCH100" ? Stockitem = quoteItem.BodyType.Longitudinal : String.Empty),
        Quantity = 2,
        Length = globals.FloorCalculatedLength
    };
0
Giorgi Nakeuri On

Try with conditional operator(https://msdn.microsoft.com/en-us/library/ty67wk28.aspx):

StockItem = quoteItem.Chassis.Longitudinal == "SCH100" ? quoteItem.BodyType.Longitudinal : null,
1
Grant Thomas On

You can use the conditional ternary operator:

The ?: operator can be used as a shortcut for an if...else statement. It is typically used as part of a larger expression where an if...else statement would be awkward.

StockItem = (quoteItem.Chassis.Longitudinal == "SCH100" ?
  quoteItem.BodyType.Longitudinal : null),

The format of the expression, explained:

test ? expression1 : expression2
  • test
    • Any Boolean expression.
  • expression1
    • An expression returned if test is true. May be a comma expression.
  • expression2 An expression
    • returned if test is false. May be a comma expression.