zoukankan      html  css  js  c++  java
  • ABP Linq 扩展的 WhereIf 查询内部实现

    public static class QueryableExtensions
    {
        public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, bool>> predicate)
        {
            return condition ? query.Where(predicate) : query;
        }
     
        public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, int, bool>> predicate)
        {
            return condition ? query.Where(predicate) : query;
        }
     
        public static IEnumerable<T> WhereIf<T>(this IEnumerable<T> query, bool condition, Func<T, bool> predicate)
        {
            return condition ? query.Where(predicate) : query;
        }
    }
    

      上面类中扩展了 IQueryable 与 IEnumerable 两种数据类型的方法。使用方式如下:

    repository.IQueryable<DP_Project>().WhereIf(type > 0, x => x.Type == type);

    这样就替代了原来通过 if 语句判断查询方式。

  • 相关阅读:
    福州3中集训day5
    福州三中集训day4
    福州三中集训day3
    福州三中基训day2
    福州三中集训day1
    Python3 字符串
    Python3 数字
    Python3 运算符
    Python3 基础数据类型
    Codeforces Round 253 (Div. 2)
  • 原文地址:https://www.cnblogs.com/jiangyunfeng/p/11862854.html
Copyright © 2011-2022 走看看