Exemple #1
0
        /// <summary>Constructs the query</summary>
        /// <param name="query">The query.</param>
        /// <param name="filter">The filter.</param>
        /// <typeparam name="T">The source type</typeparam>
        /// <typeparam name="T2">The result type</typeparam>
        /// <returns>The query</returns>
        protected static IQueryable <T2> ConstructQuery <T, T2>(IQueryable <T> query, IQueryFilter <T> filter)
            where T : PersistableModel <T>
        {
            (query as ObjectQuery).MergeOption = MergeOption.OverwriteChanges;
            if (filter == null)
            {
                return(query as IQueryable <T2>);
            }

            query = filter.GetFilterExpressions().Aggregate(query, (current, q) => current.Where(q));

            var f2 = filter as IQueryFilter <T, T2>;
            var q2 = query.Select <T, T2>(f2 == null ? null : f2.GetSelectExpression());

            if (f2 != null && f2.ShouldApplyDistinct())
            {
                q2 = q2.Distinct();
            }

            if (filter.RecordsAvailable == null)
            {
                filter.RecordsAvailable = q2.Count();
            }

            q2 = filter.GetIncludeExpressions().Aggregate(q2, (current, e) => current.Include(e));

            var orderBy = filter.GetSortExpressions().FirstOrDefault();

            if (orderBy != null)
            {
                q2 = q2.OrderBy(orderBy).ThenBy(filter.GetSortExpressions().Skip(1));
            }
            if (filter.FirstRecordDesired != null)
            {
                q2 = q2.Skip(filter.FirstRecordDesired.Value);
            }
            if (filter.RecordsDesired != null)
            {
                q2 = q2.Take(filter.RecordsDesired.Value);
            }
            return(q2);
        }