Min Length

Feature Is supported
Null safe
Validation
EF Core
EF 6

Validation result

Error

"Object length is lower than [{MinLength}]".

"Object length is greater than [{MinLength}]" - for negation.

Parameters

Parameter Description
MinLengthValue passed in constructor.

Info

Checks if length of candidate is greater than or equal to Min value.

Candidate must implement IEnumerable interface.
Note that, string is also a collection, and can be used with this Specification.

Usage

var spec = Specification.MinLength<int[]>(3);

spec.IsSatisfiedBy(new int[] {100, 200, 0});  // true
spec.IsSatisfiedBy(new int[] {100, 200});    // false
spec.IsSatisfiedBy(new int[0]);  // false
spec.IsSatisfiedBy(null);   // false

As property

var customerSpec = Specification.MinLength<Customer, string>(c => c.Comments, 3);

customerSpec.IsSatisfiedBy(new Customer { Comments = "VIP" });  // true
customerSpec.IsSatisfiedBy(new Customer { Comments = "0" });    // false
customerSpec.IsSatisfiedBy(new Customer { Comments = "" });  // false
customerSpec.IsSatisfiedBy(new Customer { Comments = null });   // false

Not Min Length

var spec = Specification.NotMinLength<int[]>(3);

spec.IsSatisfiedBy(new int[] {100, 200, 0});  // false
spec.IsSatisfiedBy(new int[] {100, 200});    // true
spec.IsSatisfiedBy(new int[0]);  // true
spec.IsSatisfiedBy(null);   // true

As property

var customerSpec = Specification.NotMinLength<Customer, string>(c => c.Comments, 3);

customerSpec.IsSatisfiedBy(new Customer { Comments = "VIP" });  // false
customerSpec.IsSatisfiedBy(new Customer { Comments = "0" });    // true
customerSpec.IsSatisfiedBy(new Customer { Comments = "" });  // true
customerSpec.IsSatisfiedBy(new Customer { Comments = null });   // true

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.MinLength<Customer, ICollection<Item>>(c => c.Items, 1);
    // var customers = context.Customers.Where(anyItemsSpec.GetExpression()).ToList();   // Exception!

    Specification.LinqToEntities = true;
    var anyItemsSpecFixed = Specification.MinLength<Customer, ICollection<Item>>(c => c.Items, 1);
    var customers = context.Customers.Where(anyItemsSpecFixed.GetExpression()).ToList();   // Works!
}
GitHub