zoukankan      html  css  js  c++  java
  • Linq实现between拓展

    先写一个拓展方法

    static class Ext
    {
       public static IQueryable<TSource> Between<TSource, TKey>
            (this IQueryable<TSource> source, 
             Expression<Func<TSource, TKey>> keySelector,
             TKey low, TKey high) where TKey : IComparable<TKey>
       {
           Expression key = Expression.Invoke(keySelector, 
                keySelector.Parameters.ToArray());
           Expression lowerBound = Expression.GreaterThanOrEqual
               (key, Expression.Constant(low));
           Expression upperBound = Expression.LessThanOrEqual
               (key, Expression.Constant(high));
           Expression and = Expression.AndAlso(lowerBound, upperBound);
           Expression<Func<TSource, bool>> lambda = 
               Expression.Lambda<Func<TSource, bool>>(and, keySelector.Parameters);
           return source.Where(lambda);
       }
    }

    调用

    var query = db.People.Between(person => person.Age, 18, 21);
  • 相关阅读:
    C++ 中的深入浅拷贝和深拷贝
    C++ 引用小问题
    6-10
    6-8
    6-7
    6-4
    6-3
    6-1
    5-31
    COMException 依赖服务或组无法启动(0x8007042C)处理办法
  • 原文地址:https://www.cnblogs.com/david1989/p/3675354.html
Copyright © 2011-2022 走看看