Using an 'if statement' correctly in a variable

80 views Asked by At

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!

4

There are 4 answers

3
DavidG On

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 use FirstOrDefault or you may get an exception rather than a null return value. Finally removing Where as it's simpler to write this way (good spot by @YuvalItzchakov):

var qisg = new QuoteItemSectionGroup
{
    SectionGroup = db.SectionGroups.Where(x => x.Name == "Longitudinals" && x.Section == TruckSection.Floor).First(),
    StockItem = 
        db.StockItems.FirstOrDefault(x => x.StockCode == "SCH113") ?? 
            quoteItem.Chassis.Longitudinal
    Quantity = 2,
    Length = globals.FloorCalculatedLength
};
1
Dhrumil On

First() will return an object and not a boolean expression, which is what you need here. So you'll have to make a change which is like this :

Change this line of code :

StockItem = db.StockItems.Where(x => x.StockCode == "SCH113").First() ? quoteItem.Chassis.Longitudinal : quoteItem.BodyType.Longitudinal

To this :

StockItem = db.StockItems.Where(x => x.StockCode == "SCH113").First() == null ? quoteItem.Chassis.Longitudinal : quoteItem.BodyType.Longitudinal

Now this is just an example describing what type of change you would have to make here. I have used null here. You can use also use First().SomeField == somevalue or anything like that too. Just make sure its a boolean condition and you will be fine with this.

Hope this helps.

6
Dennis_E On

If I understand the question correctly, you need something like this:

StockItem = db.StockItems.Any(x => x.StockCode == "SCH113")
? quoteItem.Chassis.Longitudinal
: quoteItem.BodyType.Longitudinal
0
David Arno On

As explained to you in your previous question on this topic, the ternary operator takes the form:

var x = <some boolean expression> ? <value assigned to x if true> : <value if false>

You however are doing:

StockItem = db.StockItems.Where(x => x.StockCode == "SCH113").First() 
            ? quoteItem.Chassis.Longitudinal 
            : quoteItem.BodyType.Longitudinal

In this case db.StockItems.Where(x => x.StockCode == "SCH113").First() doesn't return a boolean. So you need to fix this expression, presumably by comparing it with some other value.