Example #1
0
 /// <summary>
 /// 用==表示值被更改
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="obj"></param>
 /// <param name="expression"></param>
 public static void Change <T>(this T obj, Expression <Func <T, bool> > expression) where T : CRL.IModel, new()
 {
     if (expression.Body is BinaryExpression)
     {
         var Reflection        = ReflectionHelper.GetInfo <T>();
         BinaryExpression be   = ((BinaryExpression)expression.Body);
         MemberExpression mExp = (MemberExpression)be.Left;
         string           name = mExp.Member.Name;
         var    right          = be.Right;
         object value;
         if (right is ConstantExpression)
         {
             ConstantExpression cExp = (ConstantExpression)right;
             value = cExp.Value;
         }
         else
         {
             value = LambdaCompileCache.GetParameExpressionValue(right);
             //value = Expression.Lambda(right).Compile().DynamicInvoke();
         }
         //更改对象值
         var pro   = TypeCache.GetProperties(typeof(T), true);
         var field = pro[name];
         //field.TupleSetValue<T>(obj, value);
         Reflection.GetAccessor(field.MemberName).Set((T)obj, value);
         obj.SetChanges(name, value);
     }
     else
     {
         obj.Change <T, bool>(expression);
     }
 }
Example #2
0
 List <TModel> QueryFromCacheBase(Expression <Func <TModel, bool> > expression, out int total, int pageIndex = 0, int pageSize = 0)
 {
     total = 0;
     #region  KEY查找
     if (expression.Body is BinaryExpression)
     {
         var binary = expression.Body as BinaryExpression;
         if (binary.NodeType == ExpressionType.Equal)
         {
             if (binary.Left is MemberExpression)
             {
                 var member     = binary.Left as MemberExpression;
                 var primaryKey = TypeCache.GetTable(typeof(TModel)).PrimaryKey.MemberName;
                 if (member.Member.Name.ToUpper() == primaryKey.ToUpper())
                 {
                     var value = LambdaCompileCache.GetParameExpressionValue(binary.Right).ToString();
                     //var value = (int)Expression.Lambda(binary.Right).Compile().DynamicInvoke();
                     var all = GetCache(CacheQuery());
                     if (all == null)
                     {
                         return(new List <TModel>());
                     }
                     if (all.ContainsKey(value))
                     {
                         total = 1;
                         return(new List <TModel>()
                         {
                             all[value]
                         });
                     }
                     return(new List <TModel>());
                 }
             }
         }
     }
     #endregion
     var predicate = expression.Compile();
     IEnumerable <TModel> data;
     int cacheTotal = AllCache.Count();
     if (CacheQueryAsParallel && cacheTotal > 100000)
     {
         data = AllCache.AsParallel().Where(predicate);
     }
     else
     {
         data = AllCache.Where(predicate);
     }
     total = data.Count();
     if (pageIndex > 0)
     {
         //var data2 = Base.CutList(data, pageIndex, pageSize);
         var data2 = data.Page(pageIndex, pageSize).ToList();
         return(data2);
     }
     return(data.ToList());
 }
Example #3
0
        CRLExpression RouteExpressionHandler(Expression exp, int level = 0)
        {
            if (exp is BinaryExpression)
            {
                BinaryExpression be = (BinaryExpression)exp;
                level += 1;
                return(BinaryExpressionHandler(be.Left, be.Right, be.NodeType, level));
            }
            else if (exp is MemberExpression)
            {
                MemberExpression mExp = (MemberExpression)exp;
                if (mExp.Expression != null && mExp.Expression.NodeType == ExpressionType.Parameter) //like b.Name==b.Name1 或b.Name
                {
                    return(new CRLExpression()
                    {
                        Type = CRLExpressionType.Name, Data = mExp.Member.Name
                    });
                }
                //var obj = Expression.Lambda(mExp).Compile().DynamicInvoke();
                var obj = LambdaCompileCache.GetParameExpressionValue(mExp);
                if (obj is Enum)
                {
                    obj = (int)obj;
                }
                return(new CRLExpression()
                {
                    Type = CRLExpressionType.Value, Data = obj
                });
            }
            else if (exp is NewArrayExpression)
            {
                #region 数组
                NewArrayExpression naExp = (NewArrayExpression)exp;
                StringBuilder      sb    = new StringBuilder();
                foreach (Expression expression in naExp.Expressions)
                {
                    sb.AppendFormat(",{0}", RouteExpressionHandler(expression));
                }
                //return sb.Length == 0 ? "" : sb.Remove(0, 1).ToString();
                return(new CRLExpression()
                {
                    Type = CRLExpressionType.Value, Data = sb.Length == 0 ? "" : sb.Remove(0, 1).ToString()
                });

                #endregion
            }
            else if (exp is MethodCallExpression)
            {
                MethodCallExpression mcExp = (MethodCallExpression)exp;
                if (mcExp.Object is MemberExpression)
                {
                    var mExp = mcExp.Object as MemberExpression;
                    if (mExp.Expression.NodeType != ExpressionType.Parameter)
                    {
                        //not like b.BarCode.Contains("abc")
                        //按变量或常量编译值
                        var obj = LambdaCompileCache.GetParameExpressionValue(exp);
                        return(new CRLExpression()
                        {
                            Type = CRLExpressionType.Value, Data = obj
                        });
                    }
                }
                else if (mcExp.Object is ConstantExpression)
                {
                    //var cExp = mcExp.Object as ConstantExpression;
                    //like b.BarCode == aa()
                    var obj = LambdaCompileCache.GetParameExpressionValue(exp);
                    return(new CRLExpression()
                    {
                        Type = CRLExpressionType.Value, Data = obj
                    });
                }
                string methodName = mcExp.Method.Name;

                string        field = "";
                List <object> args  = new List <object>();
                if (mcExp.Object == null)
                {
                    field = RouteExpressionHandler(mcExp.Arguments[0]).ToString();
                }
                else
                {
                    field = mcExp.Object.ToString().Split('.')[1];
                    var obj = LambdaCompileCache.GetParameExpressionValue(mcExp.Arguments[0]);
                    args.Add(obj);
                    //args.Add(Expression.Lambda(mcExp.Arguments[0]).Compile().DynamicInvoke());
                }
                if (mcExp.Arguments.Count > 1)
                {
                    var obj = LambdaCompileCache.GetParameExpressionValue(mcExp.Arguments[1]);
                    args.Add(obj);
                    //args.Add(Expression.Lambda(mcExp.Arguments[1]).Compile().DynamicInvoke());
                }
                if (mcExp.Arguments.Count > 2)
                {
                    var obj = LambdaCompileCache.GetParameExpressionValue(mcExp.Arguments[2]);
                    args.Add(obj);
                    //args.Add(Expression.Lambda(mcExp.Arguments[2]).Compile().DynamicInvoke());
                }
                if (mcExp.Arguments.Count > 3)
                {
                    var obj = LambdaCompileCache.GetParameExpressionValue(mcExp.Arguments[3]);
                    args.Add(obj);
                    //args.Add(Expression.Lambda(mcExp.Arguments[3]).Compile().DynamicInvoke());
                }
                var methodCall = string.Format("{0}|{1}|{2}", field, methodName, string.Join(",", args));
                return(new CRLExpression()
                {
                    Type = CRLExpressionType.MethodCall, Data = methodCall
                });

                throw new NotSupportedException("暂不支持");
            }
            else if (exp is ConstantExpression)
            {
                #region 常量
                ConstantExpression cExp = (ConstantExpression)exp;
                object             value;
                if (cExp.Value == null)
                {
                    value = null;
                }
                else
                {
                    value = cExp.Value.ToString();
                }
                //return value;
                return(new CRLExpression()
                {
                    Type = CRLExpressionType.Value, Data = value
                });

                #endregion
            }
            else if (exp is UnaryExpression)
            {
                UnaryExpression ue = ((UnaryExpression)exp);
                level += 1;
                return(RouteExpressionHandler(ue.Operand, level));
            }
            return(null);
        }