运营商'||'不能应用于string和bool类型的操作数

时间:2015-08-19 08:41:27

标签: c# wpf linq wcf

我有一个问题,我在下面运行代码片段,它给了我错误:

  

运营商'||'不能应用于'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);

我如何能够使用与使用'||'运算符不同的编码来执行相同类型的事情?

2 个答案:

答案 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 );

变化:

  • 围绕三元的禁忌使其更具可读性。
  • quoteItem.BodyType.Name =" Royal Corrugated",这是错误的应该是两个= = =

答案 1 :(得分:1)

来自此片段:

"Royal Corrugated" || quoteItem.BodyType.Name == "Royal Smooth Glued"

==运算符优先于||操作

所以quoteItem.BodyType.Name == "Royal Smooth Glued"转换为布尔值。那么你基本上拥有C#不允许的<string> || <bool>