Пример #1
0
        internal static IQueryModifier <TEntity>[] GetModifiers <TEntity>(this QueryModifiers cmod)
        {
            if (cmod == null)
            {
                return(null);
            }

            List <IQueryModifier <TEntity> > modifiers = new List <IQueryModifier <TEntity> >();

            if (cmod.Filters != null)
            {
                modifiers.AddRange(cmod.Filters.Where(f => f != null).Select(f => f.GetModifier <TEntity>()));
            }

            if (!cmod.Sorting.IsNullOrEmpty())
            {
                var betterSort = cmod.Sorting.Where(s => s != null).ToList();
                modifiers.AddRange(betterSort.Take(1).Select(s => s.GetModifier <TEntity>(isFirst: true)));
                modifiers.AddRange(betterSort.Skip(1).Select(s => s.GetModifier <TEntity>(isFirst: false)));
            }

            if (cmod.Paging != null)
            {
                modifiers.Add(cmod.Paging.GetModifier <TEntity>());
            }

            return(modifiers.Count == 0 ? null : modifiers.ToArray());
        }
Пример #2
0
        public static long LongCount <TEntity>(this IQueryable <TEntity> query, QueryModifiers queryModifiers)
        {
            var  newQuery = query.Augment(queryModifiers);
            long count    = newQuery.Count();

            return(count);
        }
        private void LogLinqModifierName(NonEmptyString linqModifier, QueryModifiers queryModifiers)
        {
            Debug.Assert(!linqModifier.ToString().IsBlank());
            this.LogInformationData("LINQ Modifier", linqModifier);

            if (queryModifiers != null)
            {
                this.LogInformationData("Query Modifiers", queryModifiers);
            }
        }
        public IList <TEntity> Page <TEntity>(int pageIndex, int pageSize, Expression <Func <TInstance, IEnumerable <TEntity> > > sequenceExpression)
        {
            this.LogLinqModifierName("Page<TEntity>(int pageIndex, int pageSize, Expression<Func<TInstance, IEnumerable<TEntity>>> sequenceExpression)", queryModifiers: null);

            QueryModifiers mods = new QueryModifiers {
                Paging = new QueryModifiers.PagingInfo {
                    PageIndex = pageIndex, PageSize = pageSize
                }
            };

            this.Invoke(sequenceExpression, sequence => sequence.ToListWithMods(mods));
            List <TEntity> entityList = (List <TEntity>) this.ReturnedValue;

            return(entityList);
        }
Пример #5
0
        /// <summary>
        /// Returns false if query modifiers are going to modify the query,
        /// and true if no changes to the query will be made.
        /// </summary>
        /// <param name="queryModifiers"></param>
        /// <returns></returns>
        public static bool IsEmpty(this QueryModifiers queryModifiers)
        {
            if (queryModifiers != null)
            {
                if (!queryModifiers.Filters.IsNullOrEmpty())
                {
                    return(false);
                }

                if (!queryModifiers.Sorting.IsNullOrEmpty())
                {
                    return(false);
                }

                if (queryModifiers.Paging != null)
                {
                    return(false);
                }
            }

            return(true);
        }
Пример #6
0
        public static long LongCount <TEntity>(this IEnumerable <TEntity> collection, QueryModifiers queryModifiers)
        {
            var  newQuery = collection.Augment(queryModifiers);
            long count    = newQuery.Count();

            return(count);
        }
Пример #7
0
        public static List <TEntity> ToListWithMods <TEntity>(this IEnumerable <TEntity> collection, QueryModifiers queryModifiers)
        {
            var newQuery = collection.Augment(queryModifiers);

            // ReSharper disable once SuspiciousTypeConversion.Global
            List <TEntity> retVal = newQuery as List <TEntity> ?? newQuery.ToList();

            return(retVal);
        }
Пример #8
0
        public static IList <TEntity> ToIListWithMods <TEntity>(this IQueryable <TEntity> query, QueryModifiers queryModifiers)
        {
            var newQuery = query.Augment(queryModifiers);

            // ReSharper disable once SuspiciousTypeConversion.Global
            IList <TEntity> retVal = newQuery as IList <TEntity> ?? newQuery.ToList();

            return(retVal);
        }
Пример #9
0
 /// <summary>
 /// Applies dynamic modifiers to a collection.
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="collection"></param>
 /// <param name="queryModifiers"></param>
 /// <returns></returns>
 public static IEnumerable <TEntity> Augment <TEntity>(this IEnumerable <TEntity> collection, QueryModifiers queryModifiers)
 {
     IQueryModifier <TEntity>[] mods = queryModifiers.GetModifiers <TEntity>();
     return(collection.AugmentCollection(mods));
 }
Пример #10
0
 /// <summary>
 /// Applies dynamic modifiers to a query.
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="query"></param>
 /// <param name="queryModifiers"></param>
 /// <returns></returns>
 public static IQueryable <TEntity> Augment <TEntity>(this IQueryable <TEntity> query, QueryModifiers queryModifiers)
 {
     IQueryModifier <TEntity>[] mods = queryModifiers.GetModifiers <TEntity>();
     return(query.AugmentQuery(mods));
 }
        /// <summary>
        ///     Adds LongCount() to IQueryable return result.
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="sequenceExpression"></param>
        /// <param name="queryModifiers"></param>
        /// <returns>Number of items in the collection</returns>
        public long Count <TEntity>(Expression <Func <TInstance, IEnumerable <TEntity> > > sequenceExpression, QueryModifiers queryModifiers = null)
        {
            this.LogLinqModifierName("Count<TEntity>(Expression<Func<TInstance, IEnumerable<TEntity>>> sequenceExpression)", queryModifiers);
            this.Invoke(sequenceExpression, collection => LongCountInternal(collection, queryModifiers));
            long count = (long)this.ReturnedValue;

            return(count);
        }
        /// <summary>
        ///     Adds LongCount() to IQueryable return result.
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="linqQueryExpression"></param>
        /// <param name="queryModifiers"></param>
        /// <returns>Number of records that would be returned by the query</returns>
        public long Count <TEntity>(Expression <Func <TInstance, IQueryable <TEntity> > > linqQueryExpression, QueryModifiers queryModifiers = null)
        {
            this.LogLinqModifierName("Count<TEntity>(Expression<Func<TInstance, IQueryable<TEntity>>> linqQueryExpression)", queryModifiers);
            this.Invoke(linqQueryExpression, query => LongCountInternal(query, queryModifiers));
            long count = (long)this.ReturnedValue;

            return(count);
        }
 private static object LongCountInternal <TEntity>(IEnumerable <TEntity> collection, QueryModifiers queryModifiers)
 {
     return(collection.LongCount(queryModifiers));
 }
 /// <summary>
 /// Can't use QueryModifiersExtensions.LongCount()
 /// because that returns int, and we need object.
 /// </summary>
 /// <typeparam name="TEntity"></typeparam>
 /// <param name="query"></param>
 /// <param name="queryModifiers"></param>
 /// <returns></returns>
 private static object LongCountInternal <TEntity>(IQueryable <TEntity> query, QueryModifiers queryModifiers)
 {
     return(query.LongCount(queryModifiers));
 }
        /// <summary>
        ///     Appends ToList() to IEnumerable.
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="sequenceExpression"></param>
        /// <param name="queryModifiers">Optional filtering, sorting and paging applied to the query</param>
        /// <returns></returns>
        public List <TEntity> ListList <TEntity>(Expression <Func <TInstance, IEnumerable <TEntity> > > sequenceExpression, QueryModifiers queryModifiers = null)
        {
            this.LogLinqModifierName("ListList<TEntity>(Expression<Func<TInstance, IEnumerable<TEntity>>> sequenceExpression)", queryModifiers);
            this.Invoke(sequenceExpression, collection => collection.ToListWithMods(queryModifiers));
            List <TEntity> entityList = (List <TEntity>) this.ReturnedValue;

            return(entityList);
        }
        /// <summary>
        ///     Triggers query execution by appending ToList() to IQueryable, and returns List.
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="linqQueryExpression"></param>
        /// <param name="queryModifiers">Optional filtering, sorting and paging applied to the query</param>
        /// <returns></returns>
        public List <TEntity> ListList <TEntity>(Expression <Func <TInstance, IQueryable <TEntity> > > linqQueryExpression, QueryModifiers queryModifiers = null)
        {
            this.LogLinqModifierName("ListList<TEntity>(Expression<Func<TInstance, IQueryable<TEntity>>> linqQueryExpression)", queryModifiers);
            this.Invoke(linqQueryExpression, query => query.ToListWithMods(queryModifiers));
            List <TEntity> entityList = (List <TEntity>) this.ReturnedValue;

            return(entityList);
        }