score:7

Accepted answer

The first statement does not execute the actual SQL query, it only builds part of the query you intend to run.

It is when you call query.Count() that the first will be executed

SELECT COUNT(*) FROM Table WHERE Something = something

On query.Skip().Take() won't execute the query either, it is only when you try to enumerate the results(doing a foreach over paged or calling .ToList() on it) that it will execute the appropriate SQL statement retrieving only the rows for the page (using ROW_NUMBER).

If watch this in the SQL Profiler you will see that exactly two queries are executed and at no point it will try to retrieve the full table.


Be careful when you are using the debugger, because if you step after the first statement and try to look at the contents of query that will execute the SQL query. Maybe that is the source of your misunderstanding.

score:2

I believe Entity Framework might structure the SQL query with the appropriate conditions based on the linq statements. (e.g. using ROWNUMBER() OVER ...).

I could be wrong on that, however. I'd run SQL profiler and see what the generated query looks like.

score:3

// Execute the query
var query = db.Entity.Where(e => e.Something == something);

For your information, nothing is called after the first statement.

// Get the total num records
var total = query.Count();

This count query will be translated to SQL, and it'll make a call to database. This call will not get all records, because the generated SQL is something like this:

SELECT COUNT(*) FROM Entity where Something LIKE 'something'

For the last query, it doesn't get all the records neither. The query will be translated into SQL, and the paging run in the database.

Maybe you'll find this question useful: efficient way to implement paging


Related Query

More Query from same tag