【问题标题】:Syntax for checking multiple conditions in an 'if' statement在“if”语句中检查多个条件的语法
【发布时间】:2012-12-13 13:51:19
【问题描述】:

这是情况:

if count items is either 0,1,5,7,8,9,10 then string = "string one"
if count items is either 2,3,4 then string = "string two"

我尝试过(在 cs razor 视图内)

@if (@item.TotalImages == 1 || 5 || 7 || 8 || 9 || 10)
{
   string mystring = "string one"
}

但我收到此错误

运算符 ||不能应用于 bool 或 int 类型的操作数

【问题讨论】:

    标签: c#


    【解决方案1】:

    我会使用开关:

    @switch (@item.TotalImages)
    {
        case 0:
        case 1:
        case 5:
        case 7:
        case 8:
        case 9:
        case 10:
            s = "string one";
            break;
        case 2:
        case 3:
        case 4:
            s = "string two";
            break;
        default:
            throw new Exception("Unexpected image count");
    }
    

    奇怪的是,没有人推荐一本字典:

    private string stringOne = "string one";
    private string stringTwo = "string two";
    
    private Dictionary<int, string> _map = new Dictionary<int, string>
    {
        { 0, stringOne },
        { 1, stringOne },
        { 2, stringTwo },
        { 3, stringTwo },
        { 4, stringTwo },
        { 5, stringOne },
        { 7, stringOne },
        { 8, stringOne },
        { 9, stringOne },
        { 10, stringOne },
    }
    

    然后

    @var s = _map[@item.TotalImages];
    

    这种方法更容易看出,例如,您没有处理 TotalImages == 6 的情况。

    【讨论】:

    • 为什么你更喜欢 switch 语句而不是其他建议的解决方案?只是感兴趣...
    • @Holf 假设编译器可以比我更有效地完成它。此外,其他一些答案提到了可维护性。如果逻辑可能会发生变化,那么我更倾向于使用Dictionary&lt;int, string&gt;,因为它会更灵活。
    【解决方案2】:

    在“||”之间always 必须是一个表达式,可以转换为布尔值(真/假):

    @if (@item.TotalImages == 1 || @item.TotalImages == 5 || @item.TotalImages == 7 || @item.TotalImages == 8 || @item.TotalImages == 9 || @item.TotalImages == 10)
        {
           string mystring = "string one"
        }
    @else @if(@item.TotalImages == 2 || @item.TotalImages == 3 || @item.TotalImages == 4)
        {
           string mystirng = "string two"
        }
    

    【讨论】:

      【解决方案3】:

      In 扩展方法可能是这种情况的语法糖:

      public static class CLRExtensions
      {
          public static bool In<T>(this T source, params T[] list)
          {
              return list.Contains(source);
          }
      }
      

      所以基本上不用使用多个or operator,你可以简单地写:

      @if (@item.TotalImages.In(1, 5, 7, 8, 9, 10)
      {
      }
      

      【讨论】:

      • 这个应用程序很好地使用了泛型。
      【解决方案4】:

      如果您想检查所有这些可能性,您可能会得到一个非常大的“if”语句。使用 LINQ 的更简洁的方法是:

      @if ((new List<int>{ 0, 1, 5, 7, 8, 9, 10 }).Contains(@item.TotalImages))
      {
          string mystring = "string one"
      }
      

      这样,您可以更轻松地查看和维护要检查的数字列表(或者,实际上是从其他地方传递它们)。

      【讨论】:

      • HashSet&lt;T&gt;的“Contains”方法比List&lt;T&gt;的效率更高。
      【解决方案5】:

      或许

      var accepted = new HashSet<int>(new[] {1, 5, 7, 8, 9, 10});
      
      @if (accepted.Contains(item.TotalImages))
      {
         string mystring = "string one"
      }
      

      【讨论】:

        【解决方案6】:

        仔细看错误信息:

        运算符 ||不能应用于 boolint

        类型的操作数

        还有你的代码:

        @if (@item.TotalImages == 1 || 5)
        

        您正在应用 ||运算符为 bool (@item.TotalImages == 1) 和 int (5)。 “真或 5”没有意义。 'False or 5' 也没有

        基本上,您需要做的就是制作 || 的两面。运算符布尔值。

        @if (@item.TotalImages == 1 || @item.TotalImages == 5)
        

        (当然)还有很多其他巧妙的方法可以做到这一点,但这可能是最直接的。

        【讨论】:

          【解决方案7】:

          or operator 的语法有误。

          改成。

          @if (@item.TotalImages == 1 || @item.TotalImages == 5)
          {
             string mystring = "string one"
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-12-08
            • 1970-01-01
            • 1970-01-01
            • 2013-12-17
            相关资源
            最近更新 更多