Entity Framework/Linq to SQL: Skip & Take

后端 未结 6 2469
一个人的身影
一个人的身影 2020-11-27 15:19

Just curious as to how Skip & Take are supposed to work. I\'m getting the results I want to see on the client side, but when I hook up the AnjLab SQL Profiler and look a

6条回答
  •  孤城傲影
    2020-11-27 15:30

    The following works and accomplishes the simplicity I was looking for:

    public IEnumerable ListStores(Expression> sort, bool desc, int page, int pageSize, out int totalRecords)
    {
        List stores = new List();
        using (var context = new TectonicEntities())
        {
            totalRecords = context.Stores.Count();
            int skipRows = (page - 1) * pageSize;
            if (desc)
                stores = context.Stores.OrderByDescending(sort).Skip(skipRows).Take(pageSize).ToList();
            else
                stores = context.Stores.OrderBy(sort).Skip(skipRows).Take(pageSize).ToList();
        }
        return stores;
    }
    

    The main thing that fixed it for me was changing the Func sort parameter to:

    Expression> sort
    

提交回复
热议问题