【问题标题】:Something similar to sql IN statement within .NET framework?.NET 框架中类似于 sql IN 语句的东西?
【发布时间】:2009-09-03 21:41:31
【问题描述】:

我有这个功能:

    public bool IsValidProduct(int productTypeId)
    {
        bool isValid = false;

        if (productTypeId == 10 ||
            productTypeId == 11 ||
            productTypeId == 12)
        {
            isValid = true;
        }

        return isValid;
    }

但我想知道是否有更简单的方法来编写它,例如:

    public bool IsValidProduct(int productTypeId)
    {
        bool isValid = false;

        if (productTypeId.In(10,11,12))
        {
            isValid = true;
        }

        return isValid;
    }

我知道我可以编写一个扩展方法来处理这个问题,我只是好奇是否已经有一些东西或者是否有更好的方法来编写它。

【问题讨论】:

    标签: c# .net language-features


    【解决方案1】:
    new [] {10, 11, 12}.Contains(productTypeId)
    

    【讨论】:

      【解决方案2】:

      不,我不这么认为,但您可以编写一个这样的扩展方法,让您的代码完全按照编写的方式工作。

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

      虽然数组开销有点多。您可能希望为较小的固定数量的参数添加特殊情况。

      【讨论】:

      • 希望这已经在 linq 中,但这有助于使代码简洁明了
      【解决方案3】:

      嗯,你可以这样做:

      public bool IsValidProduct(int productTypeId)
      {
          bool isValid = false;
      
          if (new[] {10,11,12}.Contains(productTypeId))
          {
              isValid = true;
          }
      
          return isValid;
      }
      

      或者如果你想要同样的东西更短:

      public bool IsValidProduct(int productTypeId)
      {
          return (new[] {10,11,12}.Contains(productTypeId));
      }
      

      【讨论】:

        【解决方案4】:

        你可以按照以下方式做一些事情:

        new int[] { 10,11,12 }.Contains(productTypeID);
        

        或者进一步为 int 创建一个扩展,如下所示:

        public static bool In(this int i, params int[] ints)
        {
            return ints.Contains(i);
        }
        

        使用:

        if (productTypeID.In(10,11,12)) { ... }
        

        我不会说它们是最有效的,但可能是的。

        【讨论】:

          【解决方案5】:

          我认为框架中没有任何内容,因此您必须编写自己的扩展方法。您可以使用 linq 获得的最接近的是:

          
          if(new[] { 10, 11, 12 }.Contains(productTypeId)) { ... }
          

          【讨论】:

            【解决方案6】:

            您可以使用 .NET 3.5 中的 Contains 扩展方法。它适用于所有可枚举类型:

            if (new [] { 10, 11, 12 }.Contains(productTypeId))
            {
                ...
            }
            

            【讨论】:

              【解决方案7】:

              var validIds = new[] { 10, 11, 12 };
              validIds.Contains(productId);

              【讨论】:

                【解决方案8】:

                如果您想要的数字都在一个范围内,而不是完全“IN”功能,这将起作用,但您可能会发现它很有用。

                if (Enumerable.Range(10, 3).Contains(productTypeId))
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2011-06-22
                  • 2010-12-07
                  • 2011-04-01
                  • 2010-09-17
                  • 2011-05-01
                  • 2020-09-28
                  相关资源
                  最近更新 更多