I need to perform queries in a ADO.NET model by using LINQ.
I have an EF class for the database of my web store WebStoreEntities
and I need to find all the products, in the table Products
, that are currently under discount. The instance of WebStoreEntities
is called webStoreDB
.
I have already available a method that queries all the product from the database:
public IList<Products> GetAllProducts()
{
return webStoreDB.Products.ToList();
}
In order to write a method with signature IList<Products> GetAllDiscountProducts()
that retrieves all the discounted products, is it faster to write:
return webStoreDB.Products.Where(m => Equals(m.discounted, 1)).ToList();
or
return GetAllProducts().Where(m => Equals(m.discounted, 1)).ToList();
return webStoreDB.Products.Where(m => Equals(m.discounted, 1)).ToList();
This is faster because the filtering is done in the database where as
return GetAllProducts().Where(m => Equals(m.discounted, 1)).ToList();
filtering will be done in memory of the client. Basically LINQ statements issued against IQueriable interface will be translated to SQL when the provider is some relational database.
But if you use LINQ with IEnumerable interface then the query is executed in memory.