【问题标题】:operator '||' cannot be applied to operands of type string and bool运算符“||”不能应用于字符串和布尔类型的操作数
【发布时间】:2015-08-19 08:41:27
【问题描述】:

我在运行下面的代码 sn-p 时遇到问题,它给了我错误:

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

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

【问题讨论】:

  • 在条件周围放置大括号(...)
  • 某事运算符优先级。你试过搜索吗?
  • 看来你的意思是quoteItem.BodyType.Name == "Royal Corrugated"不只是"Royal Corrugated"
  • @Dmitry Bychenko 是的,感谢您指出我的简单愚蠢错误! :D
  • 感谢投反对票的人:(

标签: c# wpf linq wcf


【解决方案1】:

改变

(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",这个不对应该是两个=也就是==

【讨论】:

  • 感谢您的回答 :) 但是正如@DmitryBychenko 首先指出的那样,如果他作为答案发布,我会给他正确的答案,如果他没有,我会给你。
【解决方案2】:

来自这个sn-p:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-23
    • 2016-06-30
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-20
    相关资源
    最近更新 更多