您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 经营企划 > EntityFramework41实现动态多条件查询分页和排序
234567891011121314151617181920212223///summary///根据条件分页获得记录////summary///paramname=where条件/param///paramname=orderBy排序/param///paramname=ascending是否升序/param///paramname=pageIndex当前页码/param///paramname=pageSize每页大小/param///paramname=totalRecord总记录数/param///returns记录列表/returnspublicvirtualListTGetMany(ExpressionFuncT,boolwhere,stringorderBy,boolascending,intpageIndex,intpageSize,outinttotalRecord){totalRecord=0;where=where.And(u=u.Flag!=(int)Flags.Delete);varlist=dbset.Where(where);totalRecord=list.Count();if(totalRecord=0)returnnewListT();list=list.OrderBy(orderBy,ascending).Skip((pageIndex-1)*pageSize).Take(pageSize);returnlist.ToList();}动态排序扩展:+ViewCode?publicstaticIQueryableTOrderByT(thisIQueryableTsource,stringpropertyName,boolascending)whereT:class{Typetype=typeof(T);PropertyInfoproperty=type.GetProperty(propertyName);if(property==null)thrownewArgumentException(propertyName,NotExist);ParameterExpressionparam=Expression.Parameter(type,p);ExpressionpropertyAccessExpression=Expression.MakeMemberAccess(param,property);LambdaExpressionorderByExpression=Expression.Lambda(propertyAccessExpression,param);stringmethodName=ascending?OrderBy:OrderByDescending;MethodCallExpressionresultExp=Expression.Call(typeof(Queryable),methodName,newType[]{type,property.PropertyType},source.Expression,Expression.Quote(orderByExpression));returnsource.Provider.CreateQueryT(resultExp);}如果要通过Expression获取字段,可以使用以下代码:+ViewCode?///summary///获取对应的字段名////summary///typeparamname=TSource/typeparam///paramname=keySelector/param///returns/returnspublicstaticstringGetMemberNameTSource,TKey(ExpressionFuncTSource,TKeykeySelector){stringfieldName=null;varexp=keySelector.BodyasUnaryExpression;if(exp==null){varbody=keySelector.BodyasMemberExpression;fieldName=body.Member.Name;}else{fieldName=(exp.OperandasMemberExpression).Member.Name;}returnfieldName;}多条件组合(参见老赵相关文章):+ViewCode?///summary///统一ParameterExpression////summaryinternalclassParameterReplacer:ExpressionVisitor{publicParameterReplacer(ParameterExpressionparamExpr){this.ParameterExpression=paramExpr;}publicParameterExpressionParameterExpression{get;privateset;}publicExpressionReplace(Expressionexpr){returnthis.Visit(expr);}protectedoverrideExpressionVisitParameter(ParameterExpressionp){returnthis.ParameterExpression;}}publicstaticclassPredicateExtensionses{publicstaticExpressionFuncT,boolTrueT(){returnf=true;}publicstaticExpressionFuncT,boolFalseT(){returnf=false;}publicstaticExpressionFuncT,boolAndT(thisExpressionFuncT,boolexp_left,ExpressionFuncT,boolexp_right){varcandidateExpr=Expression.Parameter(typeof(T),candidate);varparameterReplacer=newParameterReplacer(candidateExpr);varleft=parameterReplacer.Replace(exp_left.Body);varright=parameterReplacer.Replace(exp_right.Body);varbody=Expression.And(left,right);returnExpression.LambdaFuncT,bool(body,candidateExpr);}publicstaticExpressionFuncT,boolOrT(thisExpressionFuncT,boolexp_left,ExpressionFuncT,boolexp_right){varcandidateExpr=Expression.Parameter(typeof(T),candidate);varparameterReplacer=newParameterReplacer(candidateExpr);varleft=parameterReplacer.Replace(exp_left.Body);varright=parameterReplacer.Replace(exp_right.Body);varbody=Expression.Or(left,right);returnExpression.LambdaFuncT,bool(body,candidateExpr);}}调用示例:+ViewCode?123456789101112131415publicstaticPagedListUsersDTOGetUsers(intpageIndex,intpageSize,stringorderBy,boolascending,Companyscompany,stringemail,stringnickName,bool?isAdmin,UserStatususerStatus){PagedListUsersDTOresult=newPagedListUsersDTO(pageIndex,pageSize);inttotalRecord=0;ExpressionFuncUsers,boolwhere=PredicateExtensionses.TrueUsers();if(company!=Companys.All)where=where.And(u=u.Company==(int)company);if(!string.IsNullOrEmpty(email))where=where.And(u=u.Email.Contains(email));161718192021222324252627282930if(!string.IsNullOrEmpty(nickName))where=where.And(u=u.NickName.Contains(nickName));if(isAdmin.HasValue){if(isAdmin.Value)where=where.And(u=u.IsAdmin==1);elsewhere=where.And(u=u.IsAdmin==0);}if(userStatus!=UserStatus.All)where=where.And(u=u.UserStatus==(int)userStatus);if(string.IsNullOrEmpty(orderBy))orderBy=MapHelper.GetMappedNameUsersDTO,Users(u=u.UserId);elseorderBy=MapHelper.GetMappedNameUsersDTO,Users(orderBy);ListUserslist=_usersDao.GetMany(where,orderBy,ascending,pageIndex,pageSize,outtotalRecord);result.TotalRecordCount=totalRecord;foreach(vardatainlist){result.Items.Add(Mapper.MapUsers,UsersDTO(data));}returnresult;}
本文标题:EntityFramework41实现动态多条件查询分页和排序
链接地址:https://www.777doc.com/doc-2911484 .html