We are implementing Specification Patterns with Domain Driven Design. Company architect also wants us to utilize Generic repository (not my choice). Currently require specification Pattern for Filter Where, Sorting, and Paging- 3 different ones.

Are specification patterns supposed to return IEnumerable, IQueryable, or Expression? Or does it matter? If we have separate classes for filter, page, and sort, which can be combined in any manner, shouldn't they be Iqueryable ? Just need recommendations for this.

  • 2
    I don't understand the question. The Specification Pattern description does IMHO not mention return values for which IEnumerable, IQueryable, or Expression are suitable. So could you give a more detailed example, maybe some pseudocode, of what you have in mind? (Voting to close as "unclear" until you fix this).
    – Doc Brown
    Aug 24, 2019 at 21:05
  • 1
    A specification is, on the surface, just something you pass as a parameter to a query. So return whatever type you'd return if you weren't using specifications. What you should do is get all of the involved devs and architects together and just have a quick session where you will check if you have different conceptual views of this, and then iron that out - define and reach an agreement on what is meant by specification pattern, how it should manifest itself in this particular project, and how it should all work and fit together. Aug 25, 2019 at 13:01

1 Answer 1


Let's start with Microsoft's documentation for IQueryable. It says:

The IQueryable interface inherits the IEnumerable interface so that if it represents a query, the results of that query can be enumerated. Enumeration causes the expression tree associated with an IQueryable object to be executed. The definition of "executing an expression tree" is specific to a query provider. For example, it may involve translating the expression tree to an appropriate query language for the underlying data source.

In other words, an IEnumerable represents a collection that can be enumerated, while an IQueryable represents some sort of query that can be executed. Think of IQueryable as a "state machine" of sorts; it doesn't actually do anything until you compel it to execute by calling ToList() or something that enumerates its output. So you can build up an IQueryable with successive Linq statements, and nothing will happen until you actually ask it for output.

I was looking over the C# version of the Specification Pattern on Wikipedia. It's hilariously verbose, given that all you really need is some lambda expressions:

var licenseHasNotExpired = license => license.ExpiryDate > DateTime.Now;

You can use Joseph Albahari's Predicate Builder to chain together your lambda expressions with AND and OR, and presto: you have a Specification Pattern. You can use it to build up an expression tree that gets executed when you need the answer to the questions that your rules answer.

If you use Expression Trees to do this, you can even run a Visitor over them to get log output.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.