Paging in Entity Framework

前端 未结 6 1261
情书的邮戳
情书的邮戳 2020-12-03 00:56

In Entity Framework, using LINQ to Entities, database paging is usually done in following manner:

int totalRecords = EntityContext.Context.UserSet.Count;
var         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-03 01:44

    Hmmm... the actual call that uses paging is the second one - that's a single call.

    The second call is to determine the total number of rows - that's quite a different operation, and I am not aware of any way you could combine those two distinct operations into a single database call with the Entity Framework.

    Question is: do you really need the total number of rows? What for? Is that worth a second database call or not?

    Another option you would have is to use the EntityObjectSource (in ASP.NET) and then bind this to e.g. a GridView, and enable AllowPaging and AllowSorting etc. on the GridView, and let the ASP.NET runtime handle all the nitty-gritty work of retrieving the appropriate data page and displaying it.

    Marc

提交回复
热议问题