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