WhereExpression.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.Linq.Expressions;
  17. using MongoDB.Driver.Core.Misc;
  18. namespace MongoDB.Driver.Linq.Expressions
  19. {
  20. internal sealed class WhereExpression : ExtensionExpression, ISourcedExpression
  21. {
  22. private readonly string _itemName;
  23. private readonly Expression _predicate;
  24. private readonly Expression _source;
  25. public WhereExpression(Expression source, string itemName, Expression predicate)
  26. {
  27. _source = Ensure.IsNotNull(source, nameof(source));
  28. _itemName = Ensure.IsNotNull(itemName, nameof(itemName));
  29. _predicate = Ensure.IsNotNull(predicate, nameof(predicate));
  30. }
  31. public override ExtensionExpressionType ExtensionType
  32. {
  33. get { return ExtensionExpressionType.Where; }
  34. }
  35. public Expression Predicate
  36. {
  37. get { return _predicate; }
  38. }
  39. public Expression Source
  40. {
  41. get { return _source; }
  42. }
  43. public string ItemName
  44. {
  45. get { return _itemName; }
  46. }
  47. public override Type Type
  48. {
  49. get { return _source.Type; }
  50. }
  51. public override string ToString()
  52. {
  53. return string.Format("{0}.Where({1})", _source.ToString(), _predicate.ToString());
  54. }
  55. public WhereExpression Update(Expression source, Expression predicate)
  56. {
  57. if (source != _source ||
  58. predicate != _predicate)
  59. {
  60. return new WhereExpression(source, _itemName, predicate);
  61. }
  62. return this;
  63. }
  64. protected internal override Expression Accept(ExtensionExpressionVisitor visitor)
  65. {
  66. return visitor.VisitWhere(this);
  67. }
  68. }
  69. }