NotSpecification.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Linq.Expressions;
  3. namespace Abp.Specifications
  4. {
  5. /// <summary>
  6. /// Represents the specification which indicates the semantics opposite to the given specification.
  7. /// </summary>
  8. /// <typeparam name="T">The type of the object to which the specification is applied.</typeparam>
  9. public class NotSpecification<T> : Specification<T>
  10. {
  11. private readonly ISpecification<T> _specification;
  12. /// <summary>
  13. /// Initializes a new instance of <see cref="NotSpecification{T}"/> class.
  14. /// </summary>
  15. /// <param name="specification">The specification to be reversed.</param>
  16. public NotSpecification(ISpecification<T> specification)
  17. {
  18. _specification = specification;
  19. }
  20. /// <summary>
  21. /// Gets the LINQ expression which represents the current specification.
  22. /// </summary>
  23. /// <returns>The LINQ expression.</returns>
  24. public override Expression<Func<T, bool>> ToExpression()
  25. {
  26. var expression = _specification.ToExpression();
  27. return Expression.Lambda<Func<T, bool>>(
  28. Expression.Not(expression.Body),
  29. expression.Parameters
  30. );
  31. }
  32. }
  33. }