QueryableExtensions.cs 867 B

1234567891011121314151617181920
  1. using System.Linq.Expressions;
  2. namespace Edge.Core.Core.database
  3. {
  4. public static class QueryableExtensions
  5. {
  6. /// <summary>
  7. /// 根据条件动态地应用 WHERE 子句。
  8. /// </summary>
  9. /// <typeparam name="T">实体类型。</typeparam>
  10. /// <param name="query">原始的 IQueryable 查询。</param>
  11. /// <param name="condition">是否应用 WHERE 子句的条件。</param>
  12. /// <param name="predicate">要应用的 WHERE 子句的条件表达式。</param>
  13. /// <returns>根据条件返回应用了 WHERE 子句或未应用的 IQueryable 查询。</returns>
  14. public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, bool>> predicate)
  15. {
  16. return condition ? query.Where(predicate) : query;
  17. }
  18. }
  19. }