How to use Specifications with In Memory Collections

You can use specifications on collections of objects in memory. This approach can be convenient when retrieving data doesn’t require querying a remote or out of process data store like a database. If the process does require querying external persistence, refer to the practices for using a specification with a Repository Pattern or DbContext.

A specification can be applied to an in memory collection using the Evaluate method. This method takes a single parameter of type IEnumerable<T> representing the collection on which the specification will be applied to.

public class CustomerByAgeSpec : Specification<Customer>
{
    public CustomerByAgeSpec(int age)
    {
        Query.Where(x => x.Age > age);
    }
}
IEnumerable<Customer> customers = [];

var spec = new CustomerByAgeSpec(18);
IEnumerable<Customer> filteredCustomers = spec.Evaluate(customers);

Note

The in-memory evaluation will ignore the ORM specific features in the specification. Please refer to the Features section for the list of base and ORM specific functions.