1

Do this 2 queries equal or differ in performance?

var results = ctx.Products
    .Where(x => x.Category.Name == "something")
    .Select(x => new {
        ProductId = x.Id, 
        ProductName = x.name, 
        CategoryName = x.Category.Name 
    })
    .ToList();

var results = ctx.Products
    .Select(x => new {
        ProductId = x.Id, 
        ProductName = x.name, 
        CategoryName = x.Category.Name 
    })
    .Where(x => x.CategoryName == "something")  
    .ToList();

Would SQL Server use the correct index in the second query?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Reynaldi
  • 1,125
  • 2
  • 19
  • 41
  • 4
    Try http://stackoverflow.com/a/20751723/1336590 to look at the resulting sql statements if they're different, run them both in a profiler and compare running times. – Corak Jul 29 '15 at 08:52
  • I've tested this with express profiler with 1000 records in my local machine. They're almost the same in running times. I need to know if they're using the same index to make sure. – Reynaldi Jul 31 '15 at 17:17

1 Answers1

0

They are equivelent in all but Syntax. Entity Framework will only resolve the result set at iteration time, and you've basically applied the same set of filters, just in a different order.

Steve Gray
  • 450
  • 2
  • 4