QueryableExtensions.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. namespace Ips.Library.Basic
  5. {
  6. public static class QueryableExtensions
  7. {
  8. public static IQueryable<T> PageBy<T>(this IQueryable<T> query, int skipCount, int maxResultCount)
  9. {
  10. return query.Skip(skipCount).Take(maxResultCount);
  11. }
  12. public static TQueryable PageBy<T, TQueryable>(this TQueryable query, int skipCount, int maxResultCount)
  13. where TQueryable : IQueryable<T>
  14. {
  15. return (TQueryable)query.Skip(skipCount).Take(maxResultCount);
  16. }
  17. public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, bool>> predicate)
  18. {
  19. return condition
  20. ? query.Where(predicate)
  21. : query;
  22. }
  23. public static TQueryable WhereIf<T, TQueryable>(this TQueryable query, bool condition, Expression<Func<T, bool>> predicate)
  24. where TQueryable : IQueryable<T>
  25. {
  26. return condition
  27. ? (TQueryable)query.Where(predicate)
  28. : query;
  29. }
  30. public static IQueryable<T> WhereIf<T>(this IQueryable<T> query, bool condition, Expression<Func<T, int, bool>> predicate)
  31. {
  32. return condition
  33. ? query.Where(predicate)
  34. : query;
  35. }
  36. public static TQueryable WhereIf<T, TQueryable>(this TQueryable query, bool condition, Expression<Func<T, int, bool>> predicate)
  37. where TQueryable : IQueryable<T>
  38. {
  39. return condition
  40. ? (TQueryable)query.Where(predicate)
  41. : query;
  42. }
  43. }
  44. }