Feature | Is supported |
---|---|
Null safe | |
Validation | |
EF Core | |
EF 6 |
Validation result
Error
"All elements are not specified".
Also Specification adds prefix with element index, for each error returned from element Specification.
Parameters
Parameter | Description |
---|---|
SpecificationForAny | Specification object passed in constructor. |
Info
Checks if ISpecification<T>
is satisfied by ANY element in candidate collection.
Any for empty collections are always invalid (return false).
Candidate must implement IEnumerable<>
interface.
Note that, string
is also a collection, and can be used with this Specification.
Usage
var spec = Specification.Any<int[], int>(
Specification.LessThanOrEqual(0));
spec.IsSatisfiedBy(new int[] {100, 200, 0}); // true
spec.IsSatisfiedBy(new int[] {100, 200, 300}); // false
spec.IsSatisfiedBy(new int[0]); // false
spec.IsSatisfiedBy(null); // false
As property
var customerSpec = Specification.Any<Customer, char>(c => c.Comments,
Specification.Equal('0'));
customerSpec.IsSatisfiedBy(new Customer { Comments = "VIP" }); // false
customerSpec.IsSatisfiedBy(new Customer { Comments = "0" }); // true
customerSpec.IsSatisfiedBy(new Customer { Comments = "" }); // false
customerSpec.IsSatisfiedBy(new Customer { Comments = null }); // false
EF 6 support
In Linq to entities compare collections with null generates NotSupportedException
.
To prevent this, use linqToEntities
constructor flag or global Specification.LinqToEntities
flag.
using (var context = new EfDbContext())
{
var anyItemsSpec = Specification.Any<Customer, Item>(c => c.Items,
Specification.Empty<Item, int>(i => i.ItemId));
// var customers = context.Customers.Where(anyItemsSpec.GetExpression()).ToList(); // Exception!
Specification.LinqToEntities = true;
var anyItemsSpecFixed = Specification.Any<Customer, Item>(c => c.Items,
Specification.Empty<Item, int>(i => i.ItemId));
var customers = context.Customers.Where(anyItemsSpecFixed.GetExpression()).ToList(); // Works!
}