AndNotSpecification.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. using System;
  2. using System.Linq.Expressions;
  3. namespace Abp.Specifications
  4. {
  5. /// <summary>
  6. /// Represents the combined specification which indicates that the first specification
  7. /// can be satisifed by the given object whereas the second one cannot.
  8. /// </summary>
  9. /// <typeparam name="T">The type of the object to which the specification is applied.</typeparam>
  10. public class AndNotSpecification<T> : CompositeSpecification<T>
  11. {
  12. /// <summary>
  13. /// Constructs a new instance of <see cref="AndNotSpecification{T}"/> class.
  14. /// </summary>
  15. /// <param name="left">The first specification.</param>
  16. /// <param name="right">The second specification.</param>
  17. public AndNotSpecification(ISpecification<T> left, ISpecification<T> right) : base(left, right) { }
  18. /// <summary>
  19. /// Gets the LINQ expression which represents the current specification.
  20. /// </summary>
  21. /// <returns>The LINQ expression.</returns>
  22. public override Expression<Func<T, bool>> ToExpression()
  23. {
  24. var rightExpression = Right.ToExpression();
  25. var bodyNot = Expression.Not(rightExpression.Body);
  26. var bodyNotExpression = Expression.Lambda<Func<T, bool>>(bodyNot, rightExpression.Parameters);
  27. return Left.ToExpression().And(bodyNotExpression);
  28. }
  29. }
  30. }