OrderByExpression.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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.Collections.Generic;
  16. using System.Collections.ObjectModel;
  17. using System.Linq;
  18. using System.Linq.Expressions;
  19. using MongoDB.Driver.Core.Misc;
  20. namespace MongoDB.Driver.Linq.Expressions
  21. {
  22. internal sealed class OrderByExpression : ExtensionExpression, ISourcedExpression
  23. {
  24. private readonly Expression _source;
  25. private readonly ReadOnlyCollection<OrderByClause> _clauses;
  26. public OrderByExpression(Expression source, IEnumerable<OrderByClause> clauses)
  27. {
  28. _source = Ensure.IsNotNull(source, nameof(source));
  29. _clauses = Ensure.IsNotNull(clauses, "clauses") as ReadOnlyCollection<OrderByClause>;
  30. if (_clauses == null)
  31. {
  32. _clauses = new List<OrderByClause>(clauses).AsReadOnly();
  33. }
  34. }
  35. public ReadOnlyCollection<OrderByClause> Clauses
  36. {
  37. get { return _clauses; }
  38. }
  39. public Expression Source
  40. {
  41. get { return _source; }
  42. }
  43. public override ExtensionExpressionType ExtensionType
  44. {
  45. get { return ExtensionExpressionType.OrderBy; }
  46. }
  47. public override string ToString()
  48. {
  49. var clauseStrings = string.Join(", ", _clauses.Select(c => c.ToString()));
  50. return string.Format("{0}.OrderBy({1})", _source.ToString(), clauseStrings);
  51. }
  52. public OrderByExpression Update(Expression source, ReadOnlyCollection<OrderByClause> clauses)
  53. {
  54. if (source != _source ||
  55. clauses != _clauses)
  56. {
  57. return new OrderByExpression(source, clauses);
  58. }
  59. return this;
  60. }
  61. protected internal override Expression Accept(ExtensionExpressionVisitor visitor)
  62. {
  63. return visitor.VisitOrderBy(this);
  64. }
  65. }
  66. }