代码之家  ›  专栏  ›  技术社区  ›  CareTaker22

运算符“||”不能应用于string和bool类型的操作数

  •  -1
  • CareTaker22  · 技术社区  · 10 年前

    我在运行下面的代码段时遇到问题,并给出错误:

    运算符“||”不能应用于“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 回复  |  直到 10 年前
        1
  •  2
  •   sertsedat Bioscom    10 年前

    改变

    (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。名称=“皇家波纹”,这是错误的,应该是两个=哪个是==
        2
  •  1
  •   Andrew Shepherd    10 年前

    这来自以下片段:

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

    ==运算符优先于||运算符。

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