0

What is the difference between

dbContext.foo.Count(q=>q==true);

and

dbContext.foo.Where(q=>q==true).Count();

is there a difference between the performance ?

I Have Stumbled Upon This Documentation but it isn't clear enough for me . Any Help would be greatly appreciated :)

Edit: I found out that from this Question that the difference is minimal when it comes to list, how about when it is directly being queried from the database?

xSkrappy
  • 747
  • 5
  • 17
  • 1
    The both query will be same `select count from tablename where somecondition` Both have almost identical performance impacts You could inspect the EF query generated using SQL profiler. – Eldho Sep 17 '18 at 05:42

1 Answers1

2

No.

From your code example, it appears like this is in the context of Entity Framework. So the LINQ is not executed at all, but translated to SQL and executed by the SQL server. Both forms should translate the same, but you can verify that through a profiler, depending on your particular database engine.

Tsahi Asher
  • 1,767
  • 15
  • 28
  • Actually I have shorten the code , it is actually including 8 tables and yeah . Using the profiler I had found out its behavior , and it indeed is storing the data in one large variable. Thus counting the number of data which is around 10Million , is causing a large performance difference between the 2 method. Thank you so much :) – xSkrappy Sep 17 '18 at 05:46