代码之家  ›  专栏  ›  技术社区  ›  Peter C

如何使用.net实现SQL IN()的等效功能

  •  19
  • Peter C  · 技术社区  · 14 年前

    i、 e.值(1、2、4、7)

    而不是:

    值=1或值=2或值=4或值=7

    9 回复  |  直到 9 年前
        1
  •  28
  •   chue x    8 年前
    using System;
    using System.Linq;
    
    static class SqlStyleExtensions
    {
        public static bool In(this string me, params string[] set)
        {
           return set.Contains(me);
        }
    }
    

    用法:

    if (Variable.In("AC", "BC", "EA"))
    {
    
    } 
    
        2
  •  13
  •   wasatz    14 年前

    /// <summary>
    /// Returns true if the value is represented in the provided enumeration.
    /// </summary>
    /// <typeparam name="T">Type of the value</typeparam>
    /// <param name="obj">The object to check if the enumeration contains</param>
    /// <param name="values">The enumeration that might contain the object</param>
    /// <returns>True if the object exists in the enumeration</returns>
    public static bool In<T>(this T obj, IEnumerable<T> values) {
        return values.Contains(obj);
    }
    

    编辑: 不过,我会在这里邮寄,因为这是一个更通用的版本。

        3
  •  8
  •   Community Dunja Lalic    7 年前

    public static IQueryable<T> WhereIn<T, TValue>(
                    this IQueryable<T> query,
                    Expression<Func<T, TValue>> selector, 
                    params TValue[] collection) where T : class
    {
        if (selector == null) throw new ArgumentNullException("selector");
        if (collection == null) throw new ArgumentNullException("collection");
        ParameterExpression p = selector.Parameters.Single();
    
        if (!collection.Any()) return query;
    
        IEnumerable<Expression> equals = collection.Select(value =>
           (Expression)Expression.Equal(selector.Body,
                Expression.Constant(value, typeof(TValue))));
    
        Expression body = equals.Aggregate(Expression.Or);
        return query.Where(Expression.Lambda<Func<T, bool>>(body, p));
    }
    

    其中:

    public static IQueryable<T> WhereNotIn<T, TValue>(
                    this IQueryable<T> query, 
                    Expression<Func<T, TValue>> selector, 
                    params TValue[] collection) where T : class
    {
        if (selector == null) throw new ArgumentNullException("selector");
        if (collection == null) throw new ArgumentNullException("collection");
        ParameterExpression p = selector.Parameters.Single();
    
        if (!collection.Any()) return query;
    
        IEnumerable<Expression> equals = collection.Select(value =>
           (Expression)Expression.NotEqual(selector.Body,
                Expression.Constant(value, typeof(TValue))));
    
        Expression body = equals.Aggregate(Expression.And);
    
        return query.Where(Expression.Lambda<Func<T, bool>>(body, p));
    }
    

    用法:

    var args = new [] { 1, 2, 3 };
    var bookings = _repository.Find(r => r.id > 0).WhereIn(x => x.BookingTypeID, args);
    // OR we could just as easily plug args in as 1,2,3 as it's defined as params
    var bookings2 = _repository.Find(r => r.id > 0).WhereIn(x => x.BookingTypeID, 1,2,3,90);
    
    var bookings3 = _repository.Find(r => r.id > 0).WhereNotIn(x => x.BookingTypeID, 20,30,60);
    

    这真的让我每次复习都会笑:)

    吉姆

    -最初来源于此处,但修改为使用iqueryable和params: 'Contains()' workaround using Linq to Entities?

        4
  •  7
  •   Justin Niessner    14 年前
    if((new int[] {1, 2, 4, 7}).Contains(value))
    {
        // Do some work.
    }
    

    public static bool In<T>(T this obj, IEnumerable<T> col)
    {
        return col.Contains(obj);
    }
    

    最初的例子是:

    if(value.In(new int[] {1, 2, 4, 7}))
    {
        // Do some work.
    }
    
        5
  •  6
  •   Carter Medlin    11 年前

    System.Linq ...

    (VB.NET)

    Enumerable.Contains({1, 2, 4, 7}, value)
    

    {1, 2, 4, 7}.Contains(value)
    

    (C#)

    Enumerable.Contains(new int[]{1, 2, 4, 7}, value);
    

    new int[] {1, 2, 4, 7}.Contains(value);
    
        6
  •  3
  •   this. __curious_geek    14 年前

        int myValue = 1;
        List<int> checkValues = new List<int> { 1, 2, 3 };
    
        if (checkValues.Contains(myValue))
            // Do something 
    
        7
  •  3
  •   Carlos Muñoz Boom    14 年前

    使用LINQ

    var q = from x in collection
            where (new int[] { 1, 2, 4, 7}).Contains(x.value)
            select x
    
        8
  •  1
  •   Albin Sunnanbo    14 年前

    HashSet<T> .

    HashSet<int> numbers = new HashSet<int> { 1, 2, 4, 7 };
    bool is5inSet = numbers.Contains(5);
    
        9
  •  1
  •   DvS    9 年前

    int[] values = new int[]{1, 2, 4, 7};
    int target = 2;
    bool contains = values.Any(v => v == target);
    

    或使用 .Contains 正如一些人所建议的那样。