Exemple #1
0
        /// <summary>
        /// 生成验证复杂字段是否重复的Lambda
        /// </summary>
        /// <param name="Entity">源数据</param>
        /// <param name="para">源数据类型</param>
        /// <returns>Where语句</returns>
        public override Expression GetExpression(T Entity, ParameterExpression para)
        {
            ParameterExpression midPara = Expression.Parameter(typeof(V), "tm2");
            //获取中间表的List
            var list = _middleExp.Compile().Invoke(Entity);

            if (list == null)
            {
                return(null);
            }
            List <Expression> allExp = new List <Expression>();
            Expression        rv     = null;

            //循环中间表数据
            foreach (var li in list)
            {
                List <Expression> innerExp = new List <Expression>();
                bool needBreak             = false;
                //循环中间表要检查重复的字段
                foreach (var SubFieldExp in _subFieldExps)
                {
                    //拼接字段表达式,使left等于类似 x.field 的形式
                    Expression left = Expression.Property(midPara, SubFieldExp.GetPropertyName());
                    //如果字段是nullable类型的,则拼接value,形成类似 x.field.Value的形式
                    if (left.Type.IsGeneric(typeof(Nullable <>)) == true)
                    {
                        left = Expression.Property(left, "Value");
                    }
                    //如果字段是string类型,则拼接trim,形成类似 x.field.Trim()的形式
                    if (left.Type == typeof(string))
                    {
                        left = Expression.Call(left, typeof(String).GetMethod("Trim", Type.EmptyTypes));
                    }
                    //使用当前循环的中间表的数据获取字段的值
                    object vv = SubFieldExp.Compile().Invoke(li);
                    //如果值为空则跳过
                    if (vv == null)
                    {
                        needBreak = true;
                        continue;
                    }
                    //如果值为空字符串且没要求必填,则跳过
                    if (vv is string && vv.ToString() == "")
                    {
                        var requiredAttrs = li.GetType().GetSingleProperty(SubFieldExp.GetPropertyName()).GetCustomAttributes(typeof(RequiredAttribute), false).ToList();

                        if (requiredAttrs == null || requiredAttrs.Count == 0)
                        {
                            needBreak = true;
                            continue;
                        }
                        else
                        {
                            var requiredAtt = requiredAttrs[0] as RequiredAttribute;
                            if (requiredAtt.AllowEmptyStrings == true)
                            {
                                needBreak = true;
                                continue;
                            }
                        }
                    }
                    //如果值为字符串,调用trim函数
                    if (vv is string)
                    {
                        vv = vv.ToString().Trim();
                    }
                    //拼接形成 x.field == value的形式
                    ConstantExpression right = Expression.Constant(vv);
                    BinaryExpression   equal = Expression.Equal(left, right);
                    innerExp.Add(equal);
                }
                if (needBreak)
                {
                    continue;
                }
                //拼接多个 x.field==value,形成 x.field==value && x.field1==value1 .....的形式
                Expression exp = null;
                if (innerExp.Count == 1)
                {
                    exp = innerExp[0];
                }
                if (innerExp.Count > 1)
                {
                    exp = Expression.And(innerExp[0], innerExp[1]);
                    for (int i = 2; i < innerExp.Count; i++)
                    {
                        exp = Expression.And(exp, innerExp[i]);
                    }
                }
                //调用any函数,形成 .Any(x=> x.field==value && x.field1==value1....)的形式
                if (exp != null)
                {
                    var any = Expression.Call(
                        typeof(Enumerable),
                        "Any",
                        new Type[] { typeof(V) },
                        Expression.Property(para, _middleExp.GetPropertyName()),
                        Expression.Lambda <Func <V, bool> >(exp, new ParameterExpression[] { midPara }));
                    allExp.Add(any);
                }
            }
            //拼接多个any函数形成 .Any(x=> x.field==value && x.field1==value1....) || .Any(x=> x.field==value && x.field1==value1....)的形式并返回
            if (allExp.Count == 1)
            {
                rv = allExp[0];
            }
            if (allExp.Count > 1)
            {
                rv = Expression.OrElse(allExp[0], allExp[1]);
                for (int i = 2; i < allExp.Count; i++)
                {
                    rv = Expression.OrElse(rv, allExp[i]);
                }
            }
            return(rv);
        }
Exemple #2
0
        public Expression GetExpression(T Entity, ParameterExpression para)
        {
            ParameterExpression midPara = Expression.Parameter(typeof(V), "tm2");
            var list = MiddleExp.Compile().Invoke(Entity);

            List <Expression> allExp = new List <Expression>();
            Expression        rv     = null;

            foreach (var li in list)
            {
                List <Expression> innerExp = new List <Expression>();
                bool needBreak             = false;
                foreach (var SubFieldExp in SubFieldExps)
                {
                    Expression left = Expression.Property(midPara, UtilsTool.GetPropertyName(SubFieldExp));
                    if (left.Type.IsGenericType && left.Type.GetGenericTypeDefinition() == typeof(Nullable <>))
                    {
                        left = Expression.Property(left, "Value");
                    }
                    if (left.Type == typeof(string))
                    {
                        left = Expression.Call(left, typeof(String).GetMethod("Trim", Type.EmptyTypes));
                    }
                    object vv = SubFieldExp.Compile().Invoke(li);
                    if (vv == null)
                    {
                        needBreak = true;
                        continue;
                    }
                    if (vv is string && vv.ToString() == "")
                    {
                        var requiredAttrs = li.GetType().GetProperty(UtilsTool.GetPropertyName(SubFieldExp)).GetCustomAttributes(typeof(RequiredAttribute), false);

                        if (requiredAttrs == null || requiredAttrs.Length == 0)
                        {
                            needBreak = true;
                            continue;
                        }
                        else
                        {
                            var requiredAtt = requiredAttrs[0] as RequiredAttribute;
                            if (requiredAtt.AllowEmptyStrings == true)
                            {
                                needBreak = true;
                                continue;
                            }
                        }
                    }

                    if (vv is string)
                    {
                        vv = vv.ToString().Trim();
                    }
                    ConstantExpression right = Expression.Constant(vv);
                    BinaryExpression   equal = Expression.Equal(left, right);
                    innerExp.Add(equal);
                }
                if (needBreak)
                {
                    continue;
                }
                Expression exp = null;
                if (innerExp.Count == 1)
                {
                    exp = innerExp[0];
                }
                if (innerExp.Count > 1)
                {
                    exp = Expression.And(innerExp[0], innerExp[1]);
                    for (int i = 2; i < innerExp.Count; i++)
                    {
                        exp = Expression.And(exp, innerExp[i]);
                    }
                }
                if (exp != null)
                {
                    var any = Expression.Call(
                        typeof(Enumerable),
                        "Any",
                        new Type[] { typeof(V) },
                        Expression.Property(para, UtilsTool.GetPropertyName(MiddleExp)),
                        Expression.Lambda <Func <V, bool> >(exp, new ParameterExpression[] { midPara }));
                    allExp.Add(any);
                }
            }
            if (allExp.Count == 1)
            {
                rv = allExp[0];
            }
            if (allExp.Count > 1)
            {
                rv = Expression.Or(allExp[0], allExp[1]);
                for (int i = 2; i < allExp.Count; i++)
                {
                    rv = Expression.Or(rv, allExp[i]);
                }
            }
            return(rv);
        }