Example #1
0
        protected override Expression VisitMethodCall(MethodCallExpression node)
        {
            if (node.Arguments[0].NodeType == ExpressionType.Call)
            {
                this.Visit(node.Arguments[0]);
            }

            if (node.Method.Name.Equals("FirstOrDefault", StringComparison.OrdinalIgnoreCase) ||
                node.Method.Name.Equals("First", StringComparison.OrdinalIgnoreCase) ||
                node.Method.Name.Equals("SingleOrDefault", StringComparison.OrdinalIgnoreCase) ||
                node.Method.Name.Equals("Single", StringComparison.OrdinalIgnoreCase))
            {
                sqlQueryBuilder.AddTake(TypeSystem.GetElementType(node.Arguments[0].Type), "1");
            }

            string arg = null;

            if (node.Arguments.Count == 2)
            {
                arg = new LinqToSqlInterpretator()
                      .Visit(sqlQueryBuilder.Clone(), node.Arguments[1])
                      .SqlQuery;
            }

            if (node.Method.Name.Equals("Where", StringComparison.OrdinalIgnoreCase))
            {
                sqlQueryBuilder.AddWhere(TypeSystem.GetElementType(node.Arguments[0].Type), arg);
            }

            if (node.Method.Name.Equals("OrderBy", StringComparison.OrdinalIgnoreCase))
            {
                sqlQueryBuilder.AddOrderBy(TypeSystem.GetElementType(node.Arguments[0].Type), arg);
            }

            if (node.Method.Name.Equals("Skip", StringComparison.OrdinalIgnoreCase))
            {
                sqlQueryBuilder.AddSkip(TypeSystem.GetElementType(node.Arguments[0].Type), arg);
            }

            if (node.Method.Name.Equals("Take", StringComparison.OrdinalIgnoreCase))
            {
                sqlQueryBuilder.AddTake(TypeSystem.GetElementType(node.Arguments[0].Type), arg);
            }

            if (node.Method.Name.Equals("Select", StringComparison.OrdinalIgnoreCase))
            {
                sqlQueryBuilder.AddSelect(TypeSystem.GetElementType(node.Arguments[0].Type), arg);
            }

            return(node);
        }