1

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

4 Answers 4

4

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
};
Sign up to request clarification or add additional context in comments.

5 Comments

Ah, you beat me to exactly the same answer :)
@amcdermott - Hi and thank you for your comment! :) I need to check that if my quoteItem.BodyType.Longitudinal == "SCH100" THEN it has to choose quoteItem.Chassis.Longitudinal else it should be quoteItem.BodyType.Longitudinal. I also have a small problem where it gives me an error - Operator '==' cannot be applied to operands of type 'TrucksWcf.Models.StockItem' and 'string'
If your condition is that simple, then the conditional operator is the way to go, The error you are seeing typically occurs when you are trying to compare two entirely different types - in this case StockItem and string. Perhaps you need something like SomeStockItem.Name == "stringToCompare" - though when comparing stings I think it's better to use the String class's Equals(string) method.
@amcdermott - What should I insert then here at the "param"? Also is this about right for conditional operator? if (quoteItem.BodyType.Longitudinal == "SCH100") {quoteItem.Chassis.Longitudinal} else {quoteItem.BodyType.Longitudinal} return SomeStockItem; } I am a complete newbie, sorry!
I've updated my answer - I think you don't need a method to determine the StockItem because your condition is so simple.
4

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.

1 Comment

Thanks for the correction; can only assume that both Longitudinal types are of string.
2

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

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.