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.Wherepredicate) : query;
    }
 
    public static IQueryable<T> WhereIf<T>this IQueryable<T> query, bool condition, Expression<Func<T, int, bool>> predicate)
    {
        return condition ? query.Wherepredicate) : query;
    }
 
    public static IEnumerable<T> WhereIf<T>this IEnumerable<T> query, bool condition, Func<T, bool> predicate)
    {
        return condition ? query.Wherepredicate) : query;
    }
}

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

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

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

站内搜索