1234567891011121314151617181920 |
- using System.Linq.Expressions;
- namespace Edge.Core.Core.database
- {
- public static class QueryableExtensions
- {
- /// <summary>
- /// 根据条件动态地应用 WHERE 子句。
- /// </summary>
- /// <typeparam name="T">实体类型。</typeparam>
- /// <param name="query">原始的 IQueryable 查询。</param>
- /// <param name="condition">是否应用 WHERE 子句的条件。</param>
- /// <param name="predicate">要应用的 WHERE 子句的条件表达式。</param>
- /// <returns>根据条件返回应用了 WHERE 子句或未应用的 IQueryable 查询。</returns>
- public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, bool>> predicate)
- {
- return condition ? query.Where(predicate) : query;
- }
- }
- }
|