Should I return IEnumerable or IQueryable from my DAL?

It depends on what behavior you want.

  • Returning an IList<T> tells the caller that they’ve received all of the data they’ve requested
  • Returning an IEnumerable<T> tells the caller that they’ll need to iterate over the result and it might be lazily loaded.
  • Returning an IQueryable<T> tells the caller that the result is backed by a Linq provider that can handle certain classes of queries, putting the burden on the caller to form a performant query.

While the latter gives the caller a lot of flexibility (assuming your repository fully supports it), it’s the hardest to test and, arguably, the least deterministic.

Leave a Comment