Should I return IEnumerable<T> or IQueryable<T> from my DAL?

Posted by Gary '-' on Stack Overflow See other posts from Stack Overflow or by Gary '-'
Published on 2010-06-14T17:06:46Z Indexed on 2010/06/17 3:23 UTC
Read the original article Hit count: 249

I know this could be opinion, but I'm looking for best practices.

As I understand, IQueryable implements IEnumerable, so in my DAL, I currently have method signatures like the following:

IEnumerable<Product> GetProducts();
IEnumerable<Product> GetProductsByCategory(int cateogoryId);
Product GetProduct(int productId);

Should I be using IQueryable here?

What are the pros and cons of either approach?

Note that I am planning on using the Repository pattern so I will have a class like so:

public class ProductRepository {

    DBDataContext db = new DBDataContext(<!-- connection string -->);

    public IEnumerable<Product> GetProductsNew(int daysOld) {
        return db.GetProducts()
          .Where(p => p.AddedDateTime > DateTime.Now.AddDays(-daysOld ));
    }
}

Should I change my IEnumerable<T> to IQueryable<T>? What advantages/disadvantages are there to one or the other?

© Stack Overflow or respective owner

Related posts about best-practices

Related posts about linq-to-sql