Entity framework paging with extension method is slow?

此生再无相见时 提交于 2019-12-19 07:39:18

问题


I have some problems with a slow query in Entity Framework in C#. I have created an extension method called Page to handle paging, but when I use it the query gets really slow. If I just do .Skip(page.Value * pageSize.Value).Take(pageSize.Value) instead of using Page the query gets a lot faster. I guess that doing it with Page fetches all contacts before paging. Is there a way to prevent this or am I doing something else wrong?

Query:

var contacts = db.Contacts
                        .Where(x => x.AccountID == accountID && x.Deleted == false)
                        .OrderByDescending(x => x.FirstName)
                        .ThenBy(x => x.LastName)
                        .ThenBy(x => x.CreatedDate)
                        .Page(page, pageSize);

return contacts.ToList();

Extension method:

    public static IEnumerable<T> Page<T>(this IEnumerable<T> elements, int? page, int? pageSize)
    {
        if (page.HasValue && pageSize.HasValue)
            return elements.Skip(page.Value * pageSize.Value).Take(pageSize.Value);
        else
            return elements;
    }

回答1:


Your extension method should be over IQueryable so that EF can process the expression and generate the SQL query with pagination.

Since your using IEnumerable, the Page method will invoke Skip and Take of IEnumerable. This will cause enumeration of the result of the query that was built up to that point (before the call to Page) and make the pagination in memory, over all the returned items, instead of including pagination on the DB query.



来源:https://stackoverflow.com/questions/12408439/entity-framework-paging-with-extension-method-is-slow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!