我有一个问题,我在下面运行代码片段,它给了我错误:
运营商'||'不能应用于'string'和'bool'
类型的操作数
var qisg = new QuoteItemSectionGroup
{
SectionGroup = db.SectionGroups.Where(x => x.Name == "Ali Bottom Rail" && x.Section == TruckSection.FrontEndRequirments).First(),
StockItem = quoteItem.BodyType.Name == "Royal Corrugated" ? db.StockItems.Where(x => x.StockCode == "AEX165").First() : null,
};
qisg.Quantity = qisg.StockItem == null ? 0 : 1;
//Error in the line below
qisg.Length = qisg.StockItem == null ? 0 : (double)quoteItem.ExternalWidth + quoteItem.BodyType.Name = "Royal Corrugated" || quoteItem.BodyType.Name == "Royal Smooth Glued" || quoteItem.BodyType.Name == "Smooth Folded" || quoteItem.BodyType.Name == "Royal Smooth Riveted" ? -0.136 : -0.020;
quoteItem.SectionGroups.Add(qisg);
我如何能够使用与使用'||'
运算符不同的编码来执行相同类型的事情?
答案 0 :(得分:2)
更改
(double)quoteItem.ExternalWidth + quoteItem.BodyType.Name = "Royal Corrugated" || quoteItem.BodyType.Name == "Royal Smooth Glued" || quoteItem.BodyType.Name == "Smooth Folded" || quoteItem.BodyType.Name == "Royal Smooth Riveted" ? -0.136 : -0.020;
到
(double)quoteItem.ExternalWidth + ( quoteItem.BodyType.Name == "Royal Corrugated" || quoteItem.BodyType.Name == "Royal Smooth Glued" || quoteItem.BodyType.Name == "Smooth Folded" || quoteItem.BodyType.Name == "Royal Smooth Riveted" ? -0.136 : -0.020 );
变化:
答案 1 :(得分:1)
来自此片段:
"Royal Corrugated" || quoteItem.BodyType.Name == "Royal Smooth Glued"
==运算符优先于||操作
所以quoteItem.BodyType.Name == "Royal Smooth Glued"
转换为布尔值。那么你基本上拥有C#不允许的<string> || <bool>
。