How does entity framework count total rows in one query?

entity-framework-6 pagination sql

Question

I'm now replacing some Entity Framework queries with custom SQL created with Dapper. Everything is going great so far; the only thing I'm having trouble with is how to execute effective pagination within a single database query.

This is how our current code looks:

public PagedList(IQueryable<T> source, int pageIndex, int pageSize)
{
    int total = source.Count();
    TotalCount = total;
    TotalPages = total / pageSize;

    if (total % pageSize > 0)
        TotalPages++;

    PageSize = pageSize;
    PageIndex = pageIndex;
    AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
}

If I run a debugging command over this, I can see how many rows in total Source.Count returned ().

However, when I verify the generated SQL using Glimpse, I only see one query making its way to the database. the following

SELECT TOP 30 field1, field2
FROM (
    SELECT field1, field2, row_number()
    OVER (ORDER BY [Project1].[Score] DESC) AS [row_number] WHERE ..) AS Project1
WHERE project1.row_number > 30

None that I can seeCOUNT() neither are two queries being sent, nor are there any internal expressions. I don't understand how the number of rows in the sub-query was calculated.

1
1
7/2/2015 4:38:06 PM

Popular Answer

ZZZ_tmp
1
7/2/2015 4:41:00 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow