views:

35

answers:

1

Is .Skip().Take() the only way to get paging in Entity Framework? Is there some way to select the row number in the output so I could use Where( p => p.row > 9 && p.row <21)?

select top 100 ROW_NUMBER() over (order by ID) as row, * 
from myTable

I would think the ROW_Number() field must exist in the generated SQL for it to know what rows to skip and take.

Of course, I could get around this by creating separate SQL views with that field, or I could specify the exact SQL to EF, but I really want to use the Linq Expressions.

+1  A: 

Linq to entities can work only with fields which are mapped to entities. So if you build SQL view and map column containing row_number to row field in your entity, you will be able to do that otherwise not.

Ladislav Mrnka
So I take it people overload their repository functions with skip and take parameters if they are not returning IQueryable from the repository. The problem with setting up views with row_number would be predetermining the sort order. So, the sort order would need to be handled in the repository too. I guess that's not too bad since the Orderby takes a Lambda. You would think with all these strict requirements that repositories could simply be downloaded and used. Now I am off to find that download. :) Thanks for your answer.
Dr. Zim
Yes, I believe it is standard to use `Skip` and `Take`.
Gabe
Which also implies that it's standard to delay your SQL queries until outside of your repository, which also means you run the same queries twice if you access .Count and then access the elements individually.
Dr. Zim