在变量中创建 if 语句
Creating if statement inside a variable
我正在尝试在下面的代码中创建一个“if”语句:
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 中创建这样的方法?
编辑:这就是代码现在的样子
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
};
我遇到错误:
Operator '==' cannot be applied to operands of type
'TrucksWcf.Models.StockItem' and 'string'
对不起,有些答案有点太复杂了,我看不懂0_o
ALSO 下面是将另一个 StockItem 分配给产品的示例:
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
};
尝试使用条件运算符(https://msdn.microsoft.com/en-us/library/ty67wk28.aspx):
StockItem = quoteItem.Chassis.Longitudinal == "SCH100" ? quoteItem.BodyType.Longitudinal : null,
您可以使用 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),
表达式的格式,解释:
test ? expression1 : expression2
- 测试
- 任何布尔表达式。
- 表达式1
- 如果测试为真则返回一个表达式。可能是一个逗号表达式。
- expression2 一个表达式
- 如果测试为假则返回。可能是逗号表达式。
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
};
正如其他人所说,条件运算符是完美的,如果它是一个简单的 if this..then that... otherwise something else scenario.
如果您的条件更复杂,您可以创建一个方法来检查条件和 returns 适当的值。所以像...
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.Longitudinal
的类型是 StockItem
。鉴于你的最后一个代码片段显示 StockItem
有一个 StockCode
我想你可能需要这样的东西......
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”语句:
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 中创建这样的方法?
编辑:这就是代码现在的样子
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
};
我遇到错误:
Operator '==' cannot be applied to operands of type 'TrucksWcf.Models.StockItem' and 'string'
对不起,有些答案有点太复杂了,我看不懂0_o
ALSO 下面是将另一个 StockItem 分配给产品的示例:
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
};
尝试使用条件运算符(https://msdn.microsoft.com/en-us/library/ty67wk28.aspx):
StockItem = quoteItem.Chassis.Longitudinal == "SCH100" ? quoteItem.BodyType.Longitudinal : null,
您可以使用 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),
表达式的格式,解释:
test ? expression1 : expression2
- 测试
- 任何布尔表达式。
- 表达式1
- 如果测试为真则返回一个表达式。可能是一个逗号表达式。
- expression2 一个表达式
- 如果测试为假则返回。可能是逗号表达式。
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
};
正如其他人所说,条件运算符是完美的,如果它是一个简单的 if this..then that... otherwise something else scenario.
如果您的条件更复杂,您可以创建一个方法来检查条件和 returns 适当的值。所以像...
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.Longitudinal
的类型是 StockItem
。鉴于你的最后一个代码片段显示 StockItem
有一个 StockCode
我想你可能需要这样的东西......
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
};