public Expression Sum(Expression source, LambdaExpression selector)
        {
            MethodInfoGroup group = this.queryMethods.Sum;

            MethodInfo method = GetAggregationMethod(source, selector, group, null);

            return(Expression.Call(method, source, selector));
        }
        public Expression Average(Expression source, LambdaExpression selector)
        {
            MethodInfoGroup   group   = this.queryMethods.Average;
            Func <MethodInfo> generic = () => this.queryMethods.AverageGeneric;

            MethodInfo method = GetAggregationMethod(source, selector, group, generic);

            return(Expression.Call(method, source, selector));
        }
        private static MethodInfo GetAggregationMethod(
            Expression source,
            LambdaExpression selector,
            MethodInfoGroup group,
            Func <MethodInfo> generic)
        {
            Type sourceType   = TypeHelper.GetElementType(source.Type);
            Type selectorType = selector.Body.Type;

            MethodInfo genericMethod = group[selectorType];
            MethodInfo method        = null;

            if (genericMethod == null)
            {
                genericMethod = generic.Invoke();
                method        = genericMethod.MakeGenericMethod(sourceType, selectorType);
            }
            else
            {
                method = genericMethod.MakeGenericMethod(sourceType);
            }

            return(method);
        }
Exemple #4
0
        /// <summary>
        /// Prevents a default instance of the <see cref="LinqMethodProvider" /> class from
        /// being created.
        /// </summary>
        private LinqMethodProvider()
        {
            this.select     = CreateLazy(CreateSelect);
            this.selectMany = CreateLazy(CreateSelectMany);
            this.selectManyWithResultSelector = CreateLazy(CreateSelectManyWithResultSelector);

            this.count   = CreateLazy(CreateCount);
            this.where   = CreateLazy(CreateWhere);
            this.take    = CreateLazy(CreateTake);
            this.skip    = CreateLazy(CreateSkip);
            this.groupBy = CreateLazy(CreateGroupBy);

            this.orderBy           = CreateLazy(CreateOrderBy);
            this.orderByDescending = CreateLazy(CreateOrderByDescending);
            this.thenBy            = CreateLazy(CreateThenBy);
            this.thenByDescending  = CreateLazy(CreateThenByDescending);

            this.first          = CreateLazy(CreateFirst);
            this.firstOrDefault = CreateLazy(CreateFirstOrDefault);
            this.any            = CreateLazy(CreateAny);
            this.defaultIfEmpty = CreateLazy(CreateDefaultIfEmpty);
            this.asQueryable    = CreateLazy(CreateAsQueryable);

            this.distinct  = CreateLazy(CreateDistinct);
            this.except    = CreateLazy(CreateExcept);
            this.intersect = CreateLazy(CreateIntersect);
            this.union     = CreateLazy(CreateUnion);
            this.concat    = CreateLazy(CreateConcat);

            this.sum = new MethodInfoGroup(
                Tuple.Create(typeof(int), CreateLazy(CreateSumInt)),
                Tuple.Create(typeof(int?), CreateLazy(CreateSumNInt)),
                Tuple.Create(typeof(long), CreateLazy(CreateSumLong)),
                Tuple.Create(typeof(long?), CreateLazy(CreateSumNLong)),
                Tuple.Create(typeof(float), CreateLazy(CreateSumFloat)),
                Tuple.Create(typeof(float?), CreateLazy(CreateSumNFloat)),
                Tuple.Create(typeof(double), CreateLazy(CreateSumDouble)),
                Tuple.Create(typeof(double?), CreateLazy(CreateSumNDouble)),
                Tuple.Create(typeof(decimal), CreateLazy(CreateSumDecimal)),
                Tuple.Create(typeof(decimal?), CreateLazy(CreateSumNDecimal)));

            this.min = new MethodInfoGroup(
                Tuple.Create(typeof(int), CreateLazy(CreateMinInt)),
                Tuple.Create(typeof(int?), CreateLazy(CreateMinNInt)),
                Tuple.Create(typeof(long), CreateLazy(CreateMinLong)),
                Tuple.Create(typeof(long?), CreateLazy(CreateMinNLong)),
                Tuple.Create(typeof(float), CreateLazy(CreateMinFloat)),
                Tuple.Create(typeof(float?), CreateLazy(CreateMinNFloat)),
                Tuple.Create(typeof(double), CreateLazy(CreateMinDouble)),
                Tuple.Create(typeof(double?), CreateLazy(CreateMinNDouble)),
                Tuple.Create(typeof(decimal), CreateLazy(CreateMinDecimal)),
                Tuple.Create(typeof(decimal?), CreateLazy(CreateMinNDecimal)));

            this.max = new MethodInfoGroup(
                Tuple.Create(typeof(int), CreateLazy(CreateMaxInt)),
                Tuple.Create(typeof(int?), CreateLazy(CreateMaxNInt)),
                Tuple.Create(typeof(long), CreateLazy(CreateMaxLong)),
                Tuple.Create(typeof(long?), CreateLazy(CreateMaxNLong)),
                Tuple.Create(typeof(float), CreateLazy(CreateMaxFloat)),
                Tuple.Create(typeof(float?), CreateLazy(CreateMaxNFloat)),
                Tuple.Create(typeof(double), CreateLazy(CreateMaxDouble)),
                Tuple.Create(typeof(double?), CreateLazy(CreateMaxNDouble)),
                Tuple.Create(typeof(decimal), CreateLazy(CreateMaxDecimal)),
                Tuple.Create(typeof(decimal?), CreateLazy(CreateMaxNDecimal)));

            this.average = new MethodInfoGroup(
                Tuple.Create(typeof(int), CreateLazy(CreateAvgInt)),
                Tuple.Create(typeof(int?), CreateLazy(CreateAvgNInt)),
                Tuple.Create(typeof(long), CreateLazy(CreateAvgLong)),
                Tuple.Create(typeof(long?), CreateLazy(CreateAvgNLong)),
                Tuple.Create(typeof(float), CreateLazy(CreateAvgFloat)),
                Tuple.Create(typeof(float?), CreateLazy(CreateAvgNFloat)),
                Tuple.Create(typeof(double), CreateLazy(CreateAvgDouble)),
                Tuple.Create(typeof(double?), CreateLazy(CreateAvgNDouble)),
                Tuple.Create(typeof(decimal), CreateLazy(CreateAvgDecimal)),
                Tuple.Create(typeof(decimal?), CreateLazy(CreateAvgNDecimal)));

            this.minGeneric     = CreateLazy(CreateMinGeneric);
            this.maxGeneric     = CreateLazy(CreateMaxGeneric);
            this.averageGeneric = CreateLazy(CreateAvgGeneric);
        }
        private static MethodInfo GetAggregationMethod(
            Expression source,
            LambdaExpression selector,
            MethodInfoGroup group,
            Func<MethodInfo> generic)
        {
            Type sourceType = TypeHelper.GetElementType(source.Type);
            Type selectorType = selector.Body.Type;

            MethodInfo genericMethod = group[selectorType];
            MethodInfo method = null;

            if (genericMethod == null)
            {
                genericMethod = generic.Invoke();
                method = genericMethod.MakeGenericMethod(sourceType, selectorType);
            }
            else
            {
                method = genericMethod.MakeGenericMethod(sourceType);
            }

            return method;
        }