Entity Framework Code First – Filtering And Sorting with paging (1)

You may also like...

  • karan mehra

    i need to add automapper with this.. could you please help me out..
    ?

  • how to use AddSortCriteria

  • Fernando

    HI Zoran, first of all great post!

    How to implement “ISortCriteria” and “SortCriterias”?

    thanks.

  • Luis

    HI Zoran, first of all great post! it has helped a lot on creating my own repository. Do you think you can tell me the difference between `FieldSortOrder` and `ExpressionSortCriteria` ? When should one be used vs the other? And you add a few examples on how to use them?

  • Luis

    Hi Zoran, first of all, great post! It has helped a lot in writing my own repository. Could you tell me the difference (or when to use one vs the other) for the FieldSortOrder and ExpressionSortCriteria? Also, could you add some examples on how to use them? Thank you!

  • sudhakar

    Excellect work. can you guide me how to implement sorting mechanism as I am planning to use it in my upcoming project.

    • Hi Sudhakar,
      If this article is not enough, yes we may look into some specific scenarios where i hope to be able to help you. please contact me directly with some specific questions.
      Zoran

  • Simply Brilliant. I have been struggling with this, and could never get my UoW/Repo pattern to support this in a way that did not feel dirty. Great work, and thanks for sharing.

    • Hi,
      Thanks a lot for your comment. I am really happy that you found a good solution on this blog 🙂
      I was struggling with the same thing for a couple of days and thought of sharing it. Feel free to suggest improvements.

      Zoran

  • Bala

    Hi Zoran,
    Thanks. Nice post. Helped me a lot. However I am struggling in implementing paging with my code. I am herewith posting my code. See if you can help me. Thanks in advance.
    Paging Interface
    public interface IPagination : IList
    {
    int PageIndex { get; }
    int PageSize { get; }
    int TotalCount { get; }
    int TotalPages { get; }
    bool HasPreviousPage { get; }
    bool HasNextPage { get; }
    }
    Paging Class
    public Pagination(IQueryable source, int pageIndex, int pageSize)
    {
    int totalRecords = source.Count();
    this.TotalCount = totalRecords;
    this.PageIndex = pageIndex;
    this.PageSize = pageSize;
    this.TotalPages = totalRecords / pageSize;
    if (totalRecords % pageSize > 0)
    TotalPages++;
    this.AddRange(source.Skip(PageIndex * pageSize).Take(pageSize).ToList());
    }
    public Pagination(List source, int pageIndex, int pageSize)
    {
    int totalRecords = source.Count();
    this.TotalCount = totalRecords;
    this.PageIndex = pageIndex;
    this.PageSize = pageSize;
    this.TotalPages = totalRecords / pageSize;
    if (totalRecords % pageSize > 0)
    TotalPages++;
    this.AddRange(source.Skip(PageIndex * pageSize).Take(pageSize).ToList());
    }
    public int PageIndex { get; private set; }
    public int PageSize { get; private set; }
    public int TotalCount { get; private set; }
    public int TotalPages { get; private set; }
    public bool HasPreviousPage
    {
    get { return (PageIndex > 0); }
    }
    public bool HasNextPage
    {
    get { return (PageIndex + 1 < TotalPages); }
    }
    Generic Repository Interface
    public interface IRepository where T : class
    {
    void Add(T entity);
    void Delete(T entity);
    void Update(T entity);
    IQueryable GetAll();
    T FindBy(Expression expression);
    IQueryable FilterBy(Expression expression);
    IPagination GetPaged(Expression expression,int pageIndex, int pageSize);
    }
    Repository Base
    public abstract class Repository : IRepository where T : class
    {
    private STNDataContext _stnDataContext;
    private readonly IDbSet _dbSet;
    protected Repository(IDatabaseFactory databaseFactory)
    {
    DatabaseFactory = databaseFactory;
    _dbSet = StnDataContext.Set();
    }
    protected IDatabaseFactory DatabaseFactory { get; private set; }
    public STNDataContext StnDataContext
    {
    get { return _stnDataContext ?? (_stnDataContext = new STNDataContext()); }
    }
    public void Add(T entity)
    {
    _dbSet.Add(entity);
    //_stnDataContext.Commit();
    }
    public void Delete(T entity)
    {
    _dbSet.Remove(entity);
    }
    public void Update(T entity)
    {
    _dbSet.Attach(entity);
    _stnDataContext.Entry(entity).State = EntityState.Modified;
    //_stnDataContext.Commit();
    }
    public IQueryable GetAll()
    {
    return _dbSet.ToList().AsQueryable();
    }
    public T FindBy(Expression expression)
    {
    return FilterBy(expression).FirstOrDefault();
    }
    public IQueryable FilterBy(Expression expression)
    {
    return GetAll().Where(expression).AsQueryable();
    }
    public IPagination GetPaged(Expression expression,int pageIndex, int pageSize)
    {
    //return FilterBy(expression).AsQueryable().Skip(pageIndex*pageSize).Take(pageSize).ToList();
    throw new NotImplementedException();
    }
    }
    Can you help me figure out how my GetPaged() method should be? Please comment with your suggestions.
    Thanks

    • Hi Bala,
      thank you for your comment.

      I couldn’t compile the code you’ve posted here, but I tried to interpret it, what I would do in order
      to follow your logic, is the following

      public IPagination GetPaged(Expression expression,int pageIndex, int pageSize)
      {
      var pagination = new Pagination(FilterBy(expression).AsQueryable(), pageIndex, pageSize);
      return pagination;
      }

      I hope that it helps.

      Let me know if you found the solution in the meanwhile.

      Cheers
      Zoran

brightly@mailxu.com toevs_rpo