在变量内创建if语句 [英] Creating if statement inside a variable

查看:96
本文介绍了在变量内创建if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在下面的代码内创建一个' if '语句:

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

示例:

if (quoteItem.Chassis.Longitudinal == "SCH100")
    Stockitem = quoteItem.BodyType.Longitudinal;

是否可以在 var qisg 中创建这样的方法?

Is there a way that I might be able to create a method like this in my var qisg?

编辑:这是现在的代码

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

我遇到了错误:

运算符'=='不能应用于类型的操作数 'TrucksWcf.Models.StockItem'和'string'

Operator '==' cannot be applied to operands of type 'TrucksWcf.Models.StockItem' and 'string'

很抱歉,但是有些答案对我来说太复杂了,无法理解0_o

I'm sorry but some of the answers are a bit too complex for me to understand 0_o

ALSO 以下是分配给产品的另一个StockItem的示例:

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

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

编辑:我刚刚看到了您的错误消息更新.看起来quoteItem.BodyType.LongitudinalStockItem类型.鉴于您的最后一个代码片段显示StockItem具有StockCode,我认为您可能需要这样的内容...

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

这篇关于在变量内创建if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆