private bool TryResolveOdataExpression(MethodCallNode node, out Expression expression)
        {
            expression = null;

            switch (node.Name)
            {
            case "isof":
            case "cast":
                if (node.Children.Count > 2 || node.Children.Count < 1)
                {
                    throw CreateParseException(node, "Only one or two arguments to cast operator is allowed.");
                }

                TypeNameNode castTypeArg;
                Expression   operand;

                if (node.Children.Count == 1)
                {
                    castTypeArg = node.Children[0] as TypeNameNode;
                    operand     = this.thisParam;
                }
                else
                {
                    operand     = ParseExpression(node.Children[0]);
                    castTypeArg = node.Children[1] as TypeNameNode;
                }

                if (castTypeArg == null)
                {
                    throw CreateParseException(node, "Argument to cast is required to be a type literal.");
                }

                var type = ResolveType(castTypeArg);
                if (node.Name == "cast")
                {
                    expression = Expression.Convert(operand, type);
                }
                else if (node.Name == "isof")
                {
                    expression = Expression.TypeIs(operand, type);
                }
                return(true);

            case "iif":
                ParseConditionalOperator(node, ref expression);
                return(true);
            }

            var memberCandidates = QueryFunctionMapping.GetMemberCandidates(node.Name, node.Children.Count);

            foreach (var memberMapping in memberCandidates)
            {
                if (TryResolveMemberMapping(memberMapping, node, out expression))
                {
                    return(true);
                }
            }

            return(false);
        }
예제 #2
0
        private bool TryMapKnownOdataFunction(Expression origNode,
                                              MemberInfo member,
                                              IEnumerable <Expression> arguments,
                                              out Expression odataExpression)
        {
            ReplaceQueryableMethodWithCorrespondingEnumerableMethod(ref member, ref arguments);

            QueryFunctionMapping.MemberMapping memberMapping;
            if (!QueryFunctionMapping.TryGetMemberMapping(member, out memberMapping))
            {
                odataExpression = null;
                return(false);
            }

            var odataArguments = arguments.Select(Visit).Cast <object>().ToArray();
            var callFormat     = memberMapping.PreferredCallStyle == QueryFunctionMapping.MethodCallStyle.Chained
                ? memberMapping.ChainedCallFormat
                : memberMapping.StaticCallFormat;

            odataExpression = Format(origNode, memberMapping.LocalExecutionPreferred, callFormat, odataArguments);
            return(true);
        }