internal static SqlExpression Expression(System.Linq.Expressions.Expression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            switch (expression.NodeType)
            {
            case LqExpressionType.AndAlso:
                return(AndAlso(Expression(((Binary)expression).Left), Expression(((Binary)expression).Right)));

            case LqExpressionType.Call:
                var methodCall = ((MethodCallExpression)expression);
                if (methodCall.Method.Name == "Contains" && methodCall.Method.ReturnType == typeof(bool))
                {
                    //System.String的Contants方法。
                    if (methodCall.Method.ReflectedType == typeof(System.String))
                    {
                        return(Like(Expression(methodCall.Object), Expression(methodCall.Arguments[0]), true, true));
                    }
                    //System.Linq.Enumerable的Contants方法。注:这是一个扩展方法
                    if (methodCall.Method.ReflectedType == typeof(System.Linq.Enumerable))
                    {
                        if (methodCall.Arguments[1] is MemberExpression && ((MemberExpression)methodCall.Arguments[1]).Expression.NodeType == LqExpressionType.Parameter)
                        {
                            return(In(Expression(methodCall.Arguments[1]), (CollectionExpression)Expression(methodCall.Arguments[0])));
                        }
                    }
                    //IList<>/IList的Contants方法
                    if (methodCall.Method.ReflectedType.GetInterfaces().Any(x => x == typeof(IList) || x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IList <>)))
                    {
                        if (methodCall.Arguments[0] is MemberExpression && ((MemberExpression)methodCall.Arguments[0]).Expression.NodeType == LqExpressionType.Parameter)
                        {
                            return(In(Expression(methodCall.Arguments[0]), (CollectionExpression)Expression(methodCall.Object)));
                        }
                    }
                }
                else if (methodCall.Method.Name == "StartsWith" && methodCall.Method.ReturnType == typeof(bool))
                {
                    return(Like(Expression(methodCall.Object), Expression(methodCall.Arguments[0]), true, false));
                }
                else if (methodCall.Method.Name == "EndsWith" && methodCall.Method.ReturnType == typeof(bool))
                {
                    return(Like(Expression(methodCall.Object), Expression(methodCall.Arguments[0]), false, true));
                }
                else
                {
                    if (methodCall.Method.ReturnType.IsValueType ||
                        methodCall.Method.ReturnType == typeof(System.String))
                    {
                        var obj = LqExpression.Lambda <Func <object> >(LqExpression.MakeUnary(LqExpressionType.Convert, methodCall, typeof(object))).Compile()();
                        return(Constant(obj));
                    }
                    else if (IsEnumerator(methodCall.Method.ReturnType))
                    {
                        var obj  = LqExpression.Lambda <Func <object> >(LqExpression.MakeUnary(LqExpressionType.Convert, methodCall, typeof(object))).Compile()();
                        var exps = new CollectionExpression();
                        foreach (object item in (IEnumerable)obj)
                        {
                            exps.Add(Constant(item));
                        }
                        return(exps);
                    }
                }

                throw new NotSupportedException("不支持的函数调用。");

            case LqExpressionType.Equal:
                var left  = Expression(((Binary)expression).Left);
                var right = Expression(((Binary)expression).Right);

                if (left is CollectionExpression)
                {
                    return(In(right, (CollectionExpression)left));
                }

                if (right is CollectionExpression)
                {
                    return(In(left, (CollectionExpression)right));
                }

                return(Equal(left, right));

            case LqExpressionType.GreaterThan:
                return(GreaterThan(Expression(((Binary)expression).Left), Expression(((Binary)expression).Right)));

            case LqExpressionType.GreaterThanOrEqual:
                return(GreaterThanOrEqual(Expression(((Binary)expression).Left), Expression(((Binary)expression).Right)));

            case LqExpressionType.LessThan:
                return(LessThan(Expression(((Binary)expression).Left), Expression(((Binary)expression).Right)));

            case LqExpressionType.LessThanOrEqual:
                return(LessThanOrEqual(Expression(((Binary)expression).Left), Expression(((Binary)expression).Right)));

            case LqExpressionType.NotEqual:
                var left2  = Expression(((Binary)expression).Left);
                var right2 = Expression(((Binary)expression).Right);

                if (left2 is CollectionExpression)
                {
                    return(NotIn(right2, (CollectionExpression)left2));
                }

                if (right2 is CollectionExpression)
                {
                    return(NotIn(left2, (CollectionExpression)right2));
                }

                return(NotEqual(left2, right2));

            case LqExpressionType.OrElse:
                return(OrElse(Expression(((Binary)expression).Left), Expression(((Binary)expression).Right)));

            case LqExpressionType.Add:
                return(Add(Expression(((Binary)expression).Left), Expression(((Binary)expression).Right)));

            case LqExpressionType.Divide:
                return(Divide(Expression(((Binary)expression).Left), Expression(((Binary)expression).Right)));

            case LqExpressionType.Modulo:
                return(Modulo(Expression(((Binary)expression).Left), Expression(((Binary)expression).Right)));

            case LqExpressionType.Multiply:
                return(Multiply(Expression(((Binary)expression).Left), Expression(((Binary)expression).Right)));

            case LqExpressionType.Subtract:
                return(Subtract(Expression(((Binary)expression).Left), Expression(((Binary)expression).Right)));

            case LqExpressionType.Constant:
                return(Constant(((System.Linq.Expressions.ConstantExpression)expression).Value));

            case LqExpressionType.Convert:
                return(Expression(((UnaryExpression)expression).Operand));

            case LqExpressionType.MemberAccess:

                if (((MemberExpression)expression).Expression == null)
                {
                    var obj = LqExpression.Lambda <Func <object> >(LqExpression.MakeUnary(LqExpressionType.Convert, expression, typeof(object))).Compile()();
                    return(Constant(obj));
                }

                if (((MemberExpression)expression).Expression.NodeType == LqExpressionType.Parameter)
                {
                    var propertyInfo = (PropertyInfo)PropertyExpression.ExtractMemberExpression(expression).Member;
                    var fieldInfo    = Map.GetCheckedAccessor(propertyInfo.ReflectedType).MetaInfo[propertyInfo];
                    if (fieldInfo != null)
                    {
                        return(Field(fieldInfo.PropertyName));
                    }

                    throw new NotSupportedException("实体属性未映射到字段。");
                }

                var value = LqExpression.Lambda <Func <object> >(LqExpression.MakeUnary(System.Linq.Expressions.ExpressionType.Convert, expression, typeof(object))).Compile()();
                if (value is ICollection || (!(value is string) && value is IEnumerable))
                {
                    var exps = new CollectionExpression();
                    foreach (object item in (IEnumerable)value)
                    {
                        exps.Add(Constant(item));
                    }
                    return(exps);
                }
                else
                {
                    return(Constant(value));
                }

            case LqExpressionType.NewArrayInit:
                var array           = (Array)LqExpression.Lambda <Func <object> >(LqExpression.MakeUnary(System.Linq.Expressions.ExpressionType.Convert, expression, typeof(object))).Compile()();
                var arraycollection = new CollectionExpression(array.Length);
                foreach (var item in array)
                {
                    arraycollection.Add(Constant(item));
                }
                return(arraycollection);

            case LqExpressionType.ListInit:
                var list           = (ICollection)LqExpression.Lambda <Func <object> >(LqExpression.MakeUnary(System.Linq.Expressions.ExpressionType.Convert, expression, typeof(object))).Compile()();
                var listcollection = new CollectionExpression(list.Count);
                foreach (var item in list)
                {
                    listcollection.Add(Constant(item));
                }
                return(listcollection);
            }

            throw new NotSupportedException(string.Format("不受支持的Lambda表达式类型:{0}", expression.NodeType));
        }
 public static LogicalBinaryExpression In(SqlExpression left, CollectionExpression right)
 {
     return(LogicalBinaryExpression.Expression(ExpressionType.In, left, right));
 }