GroupingQuery.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Ant.Core;
  2. using Ant.ORM;
  3. using Ant.Query.QueryExpressions;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Linq.Expressions;
  8. using System.Text;
  9. namespace Ant.Query
  10. {
  11. class GroupingQuery<T> : IGroupingQuery<T>
  12. {
  13. Queryable<T> _fromQuery;
  14. List<LambdaExpression> _groupPredicates;
  15. List<LambdaExpression> _havingPredicates;
  16. public GroupingQuery(Queryable<T> fromQuery, LambdaExpression predicate)
  17. {
  18. this._fromQuery = fromQuery;
  19. this._groupPredicates = new List<LambdaExpression>(1);
  20. this._havingPredicates = new List<LambdaExpression>();
  21. this._groupPredicates.Add(predicate);
  22. }
  23. public GroupingQuery(Queryable<T> fromQuery, List<LambdaExpression> groupPredicates, List<LambdaExpression> havingPredicates)
  24. {
  25. this._fromQuery = fromQuery;
  26. this._groupPredicates = groupPredicates;
  27. this._havingPredicates = havingPredicates;
  28. }
  29. public IGroupingQuery<T> ThenBy<K>(Expression<Func<T, K>> predicate)
  30. {
  31. List<LambdaExpression> groupPredicates = new List<LambdaExpression>(this._groupPredicates.Count + 1);
  32. groupPredicates.AddRange(this._groupPredicates);
  33. groupPredicates.Add(predicate);
  34. List<LambdaExpression> havingPredicates = new List<LambdaExpression>(this._havingPredicates.Count);
  35. havingPredicates.AddRange(this._havingPredicates);
  36. return new GroupingQuery<T>(this._fromQuery, groupPredicates, havingPredicates);
  37. }
  38. public IGroupingQuery<T> Having(Expression<Func<T, bool>> predicate)
  39. {
  40. List<LambdaExpression> groupPredicates = new List<LambdaExpression>(this._groupPredicates.Count);
  41. groupPredicates.AddRange(this._groupPredicates);
  42. List<LambdaExpression> havingPredicates = new List<LambdaExpression>(this._havingPredicates.Count + 1);
  43. havingPredicates.AddRange(this._havingPredicates);
  44. havingPredicates.Add(predicate);
  45. return new GroupingQuery<T>(this._fromQuery, groupPredicates, havingPredicates);
  46. }
  47. public IQuery<TResult> Select<TResult>(Expression<Func<T, TResult>> selector)
  48. {
  49. var e = new GroupingQueryExpression(typeof(TResult), this._fromQuery.QueryExpression, selector);
  50. e.GroupPredicates.AddRange(this._groupPredicates);
  51. e.HavingPredicates.AddRange(this._havingPredicates);
  52. return new Queryable<TResult>(this._fromQuery.DbContext, e, this._fromQuery._trackEntity);
  53. }
  54. }
  55. }