コード例 #1
0
 protected override Expression VisitConstant(ConstantExpression node)
 {
     if (_addMode == true)
     {
         if ((node.Type.IsGeneric(typeof(EntityQueryable <>)) || node.Type.IsGeneric(typeof(EnumerableQuery <>))) && node.Type.GenericTypeArguments[0] == _modelType)
         {
             ParameterExpression pe = Expression.Parameter(_modelType);
             ChangePara          cp = new ChangePara();
             //传递过来的where条件,其源类型不一定和表达式中原有的相符,所以先将新where条件的源类型修改成和query一样的
             //也就是说新的where条件可能是 x=>x.id==1, 而在表达式中不一定有x,所以要先统一
             var modifiedWhere = cp.Change(_where.Body, pe);
             var rv            = Expression.Call(
                 typeof(Queryable),
                 "Where",
                 new Type[] { _modelType },
                 node,
                 Expression.Lambda(modifiedWhere, new ParameterExpression[] { pe }));
             return(rv);
         }
     }
     return(base.VisitConstant(node));
 }
コード例 #2
0
 /// <summary>
 /// 检查方法调用类型的表达式
 /// </summary>
 /// <param name="node">表达式节点</param>
 /// <returns>修改后的表达式</returns>
 protected override Expression VisitMethodCall(MethodCallExpression node)
 {
     //如果不是添加模式,那么删除所有的order
     if (_addMode == false)
     {
         var aType = node.Arguments[0].Type;
         //如果节点是order
         if (node != null && (node.Method.Name.ToLower() == "orderby" || node.Method.Name.ToLower() == "orderbydescending") && aType.GetTypeInfo().IsGenericType)
         {
             //继续往上找到不是where的节点
             return(GetParentExpNotOrder(node));
         }
     }
     //如果是添加模式
     else
     {
         var             modelType = node.Type.GenericTypeArguments[0];
         List <SortInfo> info      = new List <SortInfo>();
         var             temp      = APIHelper.JsonDeserialize <List <SortInfo> >(_sortinfo);
         info.AddRange(temp);
         Expression rv = null;
         foreach (var item in info)
         {
             ParameterExpression pe  = Expression.Parameter(modelType);
             Expression          pro = Expression.PropertyOrField(pe, item.Property);
             Type       proType      = modelType.GetProperty(item.Property).PropertyType;
             ChangePara cp           = new ChangePara();
             cp.Change(pro, pe);
             if (item.Direction == SortDir.Asc)
             {
                 if (rv == null)
                 {
                     rv = Expression.Call(
                         typeof(Queryable),
                         "OrderBy",
                         new Type[] { modelType, proType },
                         node,
                         Expression.Lambda(pro, new ParameterExpression[] { pe }));
                 }
                 else
                 {
                     rv = Expression.Call(
                         typeof(Queryable),
                         "ThenBy",
                         new Type[] { modelType, proType },
                         rv,
                         Expression.Lambda(pro, new ParameterExpression[] { pe }));
                 }
             }
             if (item.Direction == SortDir.Desc)
             {
                 if (rv == null)
                 {
                     rv = Expression.Call(
                         typeof(Queryable),
                         "OrderByDescending",
                         new Type[] { modelType, proType },
                         node,
                         Expression.Lambda(pro, new ParameterExpression[] { pe }));
                 }
                 else
                 {
                     rv = Expression.Call(
                         typeof(Queryable),
                         "ThenByDescending",
                         new Type[] { modelType, proType },
                         rv,
                         Expression.Lambda(pro, new ParameterExpression[] { pe }));
                 }
             }
         }
         return(rv);
     }
     return(base.VisitMethodCall(node));
 }