1

I have a complicated query that select from many entity from database. but i'm facing a performance issue. the data takes a long time until it retrieved unlike the sql it takes only seconds.

Tables:

Product
productPrice (it contains more than 20k rows)
Category
Store

the query:

        var grid = from p in db.Products.ToList()
                   from productPrice in db.ProductPrices.ToList()
                   where p.ProductID == productPrice.ProductID
                   from c in db.Categories.ToList()
                   where c.CategoryID == p.ProductCategoryID
                   where c.CategoryID == subCategoryID
                   from u in db.Users.ToList()
                   where u.UserID == productPrice.UserID
                   where u.UserID == storeID
                   select new
                   {
                       ProductID = p.ProductID,
                       StoreName = u.Name,
                       ProductCateogry = c.CategoryName,
                       CategoryHsCode = c.CategoryHsCode,
                       ProductName = p.ProductName,
                       ProductBarcode = p.ProductBarcode,
                       Price = productPrice.Price,
                       PriceUnit = productPrice.Unit,
                       LoggedDate = productPrice.LoggedDate
                   };

is there any problem with the query or entity frame work select all data then it filters it?

Amr Ramadan
  • 1,259
  • 5
  • 18
  • 29

1 Answers1

2

Remove all the ToList which basically loads your database completely into memory before the Where filters the records. ToList creates a list from the table.

var grid = from p in db.Products 
           from productPrice in db.ProductPrices 
           where p.ProductID == productPrice.ProductID
           from c in db.Categories 
           where c.CategoryID == p.ProductCategoryID
           where c.CategoryID == subCategoryID
           from u in db.Users 
           where u.UserID == productPrice.UserID
           where u.UserID == storeID
           select new
           {
               ProductID = p.ProductID,
               StoreName = u.Name,
               ProductCateogry = c.CategoryName,
               CategoryHsCode = c.CategoryHsCode,
               ProductName = p.ProductName,
               ProductBarcode = p.ProductBarcode,
               Price = productPrice.Price,
               PriceUnit = productPrice.Unit,
               LoggedDate = productPrice.LoggedDate
           };

If you want a list as final result (although i would initialize a custom type instead of an anonymous type):

var gridList = grid.ToList();   // loads only the filtered records into memory

By using ToList in the query you're also using Linq-To-Objects which cannot benefit from database indexes or optimizations. So the tables won't be linked with an efficient JOIN anymore but with an inefficient (in-memory) Where using a Cartesian product.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939