Linq通用分页数据查询方法
在使用EF的过程有很多需要分页查询数据的地方,但是经常重复在输入分页的相关代码,这样即不便于维护,也增加了不少工作量。
对于通用查询有几个要点,一是要动态定义查询条件,还可以动态选择所需要的列。
1、数据查询方法
Code Snippet
- publicstaticList<TResult> Query<TEntity, TOrderBy, TResult>(thisIQueryable<TEntity> query,
- Expression<Func<TEntity, bool>> where,
- Expression<Func<TEntity, TOrderBy>> orderby,
- Expression<Func<TEntity, int, TResult>> selector,
- bool isAsc)
Code Snippet
- publicstaticList<object> Query<TEntity, TOrderBy>(thisIQueryable<TEntity> query,
- Expression<Func<TEntity, bool>> where,
- Expression<Func<TEntity, TOrderBy>> orderby,
- Func<IQueryable<TEntity>, List<object>> selector,
- bool isAsc)
Code Snippet
- publicstaticPageInfo<object> Query<TEntity, TOrderBy>(thisIQueryable<TEntity> query, int index, int pageSize,
- Expression<Func<TEntity, bool>> where,
- Expression<Func<TEntity, TOrderBy>> orderby,
- Func<IQueryable<TEntity>, List<object>> selector,
- bool isAsc)
3、分页查询方法
Code Snippet
- publicstaticPageInfo<object> Query<TEntity, TOrderBy>(thisIQueryable<TEntity> query, int index, int pageSize,
- List<Expression<Func<TEntity, bool>>> wheres,
- Expression<Func<TEntity, TOrderBy>> orderby,
- Func<IQueryable<TEntity>, List<object>> selector, bool isAsc)
Code Snippet
- publicstaticPageInfo<TResult> Query<TEntity, TOrderBy, TResult>(thisIQueryable<TEntity> query, int index, int pageSize,
- List<Expression<Func<TEntity, bool>>> wheres,
- Expression<Func<TEntity, TOrderBy>> orderby,
- Func<IQueryable<TEntity>, List<TResult>> selector, bool isAsc)
Code Snippet
- publicstaticPageInfo<TResult> Query<TEntity, TOrderBy, TResult>(thisIQueryable<TEntity> query, int index, int pageSize,
- Expression<Func<TEntity, bool>> where,
- Expression<Func<TEntity, TOrderBy>> orderby,
- Func<IQueryable<TEntity>, List<TResult>> selector,
- bool isAsc)
工具类方法:
Code Snippet
- publicclassPageInfo
- {
- publicstaticvoid CheckPageIndexAndSize(refint index, refint size)
- {
- if (index < 1)
- {
- index = 1;
- }
- if (size < 1)
- {
- size = 20;
- }
- }
- publicstaticvoid CheckPageIndexAndSize(refint index, int size, int count)
- {
- if (count >= index * size)
- {
- return;
- }
- index = count / size;
- if (count % size > 0)
- {
- index++;
- }
- if (index == 0)
- {
- index = 1;
- }
- }
- }
- publicclassPageInfo<T> : PageInfo
- {
- internal PageInfo()
- {
- DataList = newList<T>();
- }
- public PageInfo(int index, int pageSize, int count, List<T> dataList)
- {
- Index = index;
- PageSie = pageSize;
- Count = count;
- DataList = dataList;
- }
- publicint Index { get; privateset; }
- publicint PageSie { get; privateset; }
- publicint Count { get; privateset; }
- publicList<T> DataList { get; privateset; }
- publicPageInfo<T> Empty
- {
- get { returnnewPageInfo<T>(); }
- }
- }
实现代码:
Code Snippet
- publicstaticclassLinqExtent
- {
- publicstaticList<TResult> Query<TEntity, TOrderBy, TResult>(thisIQueryable<TEntity> query,
- Expression<Func<TEntity, bool>> where,
- Expression<Func<TEntity, TOrderBy>> orderby,
- Expression<Func<TEntity, int, TResult>> selector,
- bool isAsc)
- {
- if (selector == null)
- {
- thrownewArgumentNullException("selector");
- }
- var queryable = query;
- if (where != null)
- {
- queryable = queryable.Where(where);
- }
- if (orderby != null)
- {
- queryable = isAsc ? queryable.OrderBy(orderby) : queryable.OrderByDescending(orderby);
- }
- return queryable.Select(selector).ToList();
- }
- publicstaticList<object> Query<TEntity, TOrderBy>(thisIQueryable<TEntity> query,
- Expression<Func<TEntity, bool>> where,
- Expression<Func<TEntity, TOrderBy>> orderby,
- Func<IQueryable<TEntity>, List<object>> selector,
- bool isAsc)
- {
- if (selector == null)
- {
- thrownewArgumentNullException("selector");
- }
- var queryable = query;
- if (where != null)
- {
- queryable = queryable.Where(where);
- }
- if (orderby != null)
- {
- queryable = isAsc ? queryable.OrderBy(orderby) : queryable.OrderByDescending(orderby);
- }
- return selector(queryable);
- }
- publicstaticPageInfo<object> Query<TEntity, TOrderBy>(thisIQueryable<TEntity> query, int index, int pageSize,
- Expression<Func<TEntity, bool>> where,
- Expression<Func<TEntity, TOrderBy>> orderby,
- Func<IQueryable<TEntity>, List<object>> selector,
- bool isAsc)
- {
- return Query(query, index, pageSize, newList<Expression<Func<TEntity, bool>>> {where}, orderby, selector,
- isAsc);
- }
- publicstaticPageInfo<object> Query<TEntity, TOrderBy>(thisIQueryable<TEntity> query, int index, int pageSize,
- List<Expression<Func<TEntity, bool>>> wheres,
- Expression<Func<TEntity, TOrderBy>> orderby,
- Func<IQueryable<TEntity>, List<object>> selector, bool isAsc)
- {
- //if (selector == null)
- //{
- // throw new ArgumentNullException("selector");
- //}
- //PageInfo.CheckPageIndexAndSize(ref index,ref pageSize);
- //IQueryable<TEntity> queryable = query;
- //if (wheres != null)
- //{
- // wheres.ForEach(p=>queryable = queryable.Where(p));
- //}
- //int count = query.Count();
- //PageInfo.CheckPageIndexAndSize(ref index,pageSize,count);
- //if (count > 0)
- //{
- // if (orderby != null)
- // {
- // queryable = isAsc ? queryable.OrderBy(orderby) : queryable.OrderByDescending(orderby);
- // }
- // return new PageInfo<object>(index,pageSize,count,selector(queryable));
- //}
- //return new PageInfo<object>(index,pageSize,count,new List<object>());
- return Query<TEntity, TOrderBy, object>(query, index, pageSize, wheres, orderby, selector, isAsc);
- }
- publicstaticPageInfo<TResult> Query<TEntity, TOrderBy, TResult>(thisIQueryable<TEntity> query, int index, int pageSize,
- List<Expression<Func<TEntity, bool>>> wheres,
- Expression<Func<TEntity, TOrderBy>> orderby,
- Func<IQueryable<TEntity>, List<TResult>> selector, bool isAsc)
- {
- if (selector == null)
- {
- thrownewArgumentNullException("selector");
- }
- PageInfo.CheckPageIndexAndSize(ref index, ref pageSize);
- IQueryable<TEntity> queryable = query;
- if (wheres != null)
- {
- wheres.ForEach(p => queryable = queryable.Where(p));
- }
- int count = query.Count();
- PageInfo.CheckPageIndexAndSize(ref index, pageSize, count);
- if (count > 0)
- {
- if (orderby != null)
- {
- queryable = isAsc ? queryable.OrderBy(orderby) : queryable.OrderByDescending(orderby);
- }
- returnnewPageInfo<TResult>(index, pageSize, count, selector(queryable));
- }
- returnnewPageInfo<TResult>(index, pageSize, count, newList<TResult>());
- }
- publicstaticPageInfo<TResult> Query<TEntity, TOrderBy, TResult>(thisIQueryable<TEntity> query, int index, int pageSize,
- Expression<Func<TEntity, bool>> where,
- Expression<Func<TEntity, TOrderBy>> orderby,
- Func<IQueryable<TEntity>, List<TResult>> selector,
- bool isAsc)
- {
- return Query(query, index, pageSize, newList<Expression<Func<TEntity, bool>>> { where }, orderby, selector,
- isAsc);
- }
- }