GroupByExpression.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* Copyright 2015-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Collections.ObjectModel;
  18. using System.Linq;
  19. using System.Linq.Expressions;
  20. using MongoDB.Driver.Core.Misc;
  21. namespace MongoDB.Driver.Linq.Expressions
  22. {
  23. internal sealed class GroupByExpression : ExtensionExpression, ISourcedExpression
  24. {
  25. private readonly Expression _keySelector;
  26. private readonly Expression _source;
  27. private readonly ReadOnlyCollection<AccumulatorExpression> _accumulators;
  28. private readonly Type _type;
  29. public GroupByExpression(Expression source, Expression keySelector)
  30. : this(source.Type, source, keySelector, Enumerable.Empty<AccumulatorExpression>())
  31. {
  32. }
  33. public GroupByExpression(Type type, Expression source, Expression keySelector, IEnumerable<AccumulatorExpression> accumulators)
  34. {
  35. _type = Ensure.IsNotNull(type, nameof(type));
  36. _source = Ensure.IsNotNull(source, nameof(source));
  37. _keySelector = Ensure.IsNotNull(keySelector, nameof(keySelector));
  38. _accumulators = Ensure.IsNotNull(accumulators, "accumulators") as ReadOnlyCollection<AccumulatorExpression>;
  39. if (_accumulators == null)
  40. {
  41. _accumulators = accumulators.ToList().AsReadOnly();
  42. }
  43. }
  44. public ReadOnlyCollection<AccumulatorExpression> Accumulators
  45. {
  46. get { return _accumulators; }
  47. }
  48. public override ExtensionExpressionType ExtensionType
  49. {
  50. get { return ExtensionExpressionType.GroupBy; }
  51. }
  52. public Expression KeySelector
  53. {
  54. get { return _keySelector; }
  55. }
  56. public Expression Source
  57. {
  58. get { return _source; }
  59. }
  60. public override Type Type
  61. {
  62. get { return _type; }
  63. }
  64. public override string ToString()
  65. {
  66. return string.Format("{0}.GroupBy({1})", _source.ToString(), _keySelector.ToString());
  67. }
  68. public GroupByExpression Update(Expression source, Expression keySelector, IEnumerable<AccumulatorExpression> accumulators)
  69. {
  70. if (source != _source ||
  71. keySelector != _keySelector ||
  72. accumulators != _accumulators)
  73. {
  74. return new GroupByExpression(_type, source, keySelector, accumulators);
  75. }
  76. return this;
  77. }
  78. protected internal override Expression Accept(ExtensionExpressionVisitor visitor)
  79. {
  80. return visitor.VisitGroupBy(this);
  81. }
  82. }
  83. }