protected virtual DbFunctionAggregate VisitFunctionAggregate(DbFunctionAggregate aggregate)
        {
            DbFunctionAggregate result = aggregate;

            if (aggregate != null)
            {
                EdmFunction          newFunction  = this.VisitFunction(aggregate.Function);
                IList <DbExpression> newArguments = this.VisitExpressionList(aggregate.Arguments);

                Debug.Assert(newArguments.Count == 1, "Function aggregate had more than one argument?");

                if (!object.ReferenceEquals(aggregate.Function, newFunction) ||
                    !object.ReferenceEquals(aggregate.Arguments, newArguments))
                {
                    if (aggregate.Distinct)
                    {
                        result = CqtBuilder.AggregateDistinct(newFunction, newArguments[0]);
                    }
                    else
                    {
                        result = CqtBuilder.Aggregate(newFunction, newArguments[0]);
                    }
                }
            }
            return(result);
        }
        protected virtual DbAggregate VisitAggregate(DbAggregate aggregate)
        {
            // Currently only function or group aggregate are possible
            DbFunctionAggregate functionAggregate = aggregate as DbFunctionAggregate;

            if (functionAggregate != null)
            {
                return(VisitFunctionAggregate(functionAggregate));
            }

            DbGroupAggregate groupAggregate = (DbGroupAggregate)aggregate;

            return(VisitGroupAggregate(groupAggregate));
        }
        private SqlFragment HandleFunction(DbFunctionAggregate fa, SqlFragment arg)
        {
            Debug.Assert(fa.Arguments.Count == 1);

            if (fa.Function.NamespaceName != "Edm")
                throw new NotSupportedException();

            FunctionFragment fragment = new FunctionFragment();
            fragment.Name = fa.Function.Name;
            if (fa.Function.Name == "BigCount")
                fragment.Name = "Count";

            fragment.Distinct = fa.Distinct;
            fragment.Argmument = arg;
            return fragment;
            //return new CastExpression(aggregate, GetDbType(functionAggregate.ResultType.EdmType));
        }