首页 > 代码库 > Linq分页

Linq分页

 1   public IQueryable<UserInfo> LoadPageuserInfos<S>(int pageSize, int pageIndex, out int total, 2            Func<UserInfo, bool> wherelambda, Func<UserInfo, S> orderbyLambda, bool isasc) 3        { 4            total = db.UserInfo.Where(wherelambda).Count(); 5  6            if (isasc) 7            { 8                return db.UserInfo.Where(wherelambda).OrderBy<UserInfo, S>(orderbyLambda)  //OrderBy 看定义;<UserInfo, S> ,UserInfo 可以根据 9              .Skip(pageSize * (pageIndex - 1))10              .Take(pageSize).AsQueryable();11            }12            else13            {14                return db.UserInfo.Where(wherelambda).OrderByDescending<UserInfo, S>(orderbyLambda)15              .Skip(pageSize * (pageIndex - 1))16              .Take(pageSize).AsQueryable();17            }18        }

今天在写分页的时候,我才考虑,在orderby和orderbyDescending后面的<TSourse,Tkey>需不需要添加,我看了网上写的,所以就记下来了;

 public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) {            return new OrderedEnumerable<TSource, TKey>(source, keySelector, null, false);        }

上面的是F12 orderby方法的定义;后面的TSourse根据泛型接口的时候,就已经定义,

IQueryable<UserInfo> LoadPageuserInfos<S>  UserInfo-》TSourse ,S-》TKey所以后面可以不用再定义,直接使用OrderBy()......;