/// <summary>
        /// Updates the query by calling <see cref="SortBy{TEntity}(QueryBuilder{TEntity},ICollectionView)"/> and <see cref="PageBy{TEntity}(QueryBuilder{TEntity},IPagedCollectionView)"/> in sequence.
        /// </summary>
        /// <typeparam name="TEntity">The generic type of the entity</typeparam>
        /// <param name="query">The query to update</param>
        /// <param name="collectionView">The collection view to pass to each method</param>
        /// <returns>An updated query</returns>
        public static QueryBuilder <TEntity> SortAndPageBy <TEntity>(this QueryBuilder <TEntity> query, ICollectionView collectionView) where TEntity : Entity
        {
            IPagedCollectionView pagedCollectionView = collectionView as IPagedCollectionView;

            if (pagedCollectionView == null)
            {
                throw new ArgumentException(Resources.MustImplementIpcv, "collectionView");
            }

            query = CollectionViewExtensions.SortBy(query, collectionView);
            query = CollectionViewExtensions.PageBy(query, pagedCollectionView);
            return(query);
        }
        /// <summary>
        /// Orders the query using the specified group and sort descriptions.
        /// </summary>
        /// <typeparam name="TEntity">The generic type of the entity</typeparam>
        /// <param name="query">The query to sort</param>
        /// <param name="groupDescriptions">The group descriptions</param>
        /// <param name="sortDescriptions">The sort descriptions</param>
        /// <returns>A query ordered according to the specified descriptions</returns>
        public static QueryBuilder <TEntity> SortBy <TEntity>(
            this QueryBuilder <TEntity> query,
            IEnumerable <GroupDescription> groupDescriptions,
            IEnumerable <SortDescription> sortDescriptions) where TEntity : Entity
        {
            bool isFirst = true;

            // First we'll sort according to the group descriptions
            foreach (PropertyGroupDescription groupDescription in groupDescriptions.OfType <PropertyGroupDescription>().Where(d => !string.IsNullOrEmpty(d.PropertyName)))
            {
                // If we sort by the same property, we need to determine the sort direction
                bool isAscending = true;
                foreach (SortDescription sortDescription in sortDescriptions.Where(d => !string.IsNullOrEmpty(d.PropertyName)))
                {
                    if (groupDescription.PropertyName == sortDescription.PropertyName)
                    {
                        isAscending = (sortDescription.Direction == ListSortDirection.Ascending);
                        break;
                    }
                }

                query = ExpressionUtility.Sort(
                    query,
                    CollectionViewExtensions.GetSortMethodName(isFirst, isAscending),
                    ExpressionUtility.BuildPropertyExpression(typeof(TEntity), groupDescription.PropertyName));

                isFirst = false;
            }

            // Then we'll sort according to the sort descriptions
            foreach (SortDescription sortDescription in sortDescriptions.Where(d => !string.IsNullOrEmpty(d.PropertyName)))
            {
                bool isAscending = (sortDescription.Direction == ListSortDirection.Ascending);

                query = ExpressionUtility.Sort(
                    query,
                    CollectionViewExtensions.GetSortMethodName(isFirst, isAscending),
                    ExpressionUtility.BuildPropertyExpression(typeof(TEntity), sortDescription.PropertyName));

                isFirst = false;
            }

            return(query);
        }
 /// <summary>
 /// Orders the query using the group and sort descriptions of the specified <paramref name="collectionView"/>.
 /// </summary>
 /// <typeparam name="TEntity">The generic type of the entity</typeparam>
 /// <param name="query">The query to sort</param>
 /// <param name="collectionView">The view containing the descriptions to sort by</param>
 /// <returns>A query ordered according to the specified <paramref name="collectionView"/></returns>
 public static QueryBuilder <TEntity> SortBy <TEntity>(this QueryBuilder <TEntity> query, ICollectionView collectionView) where TEntity : Entity
 {
     return(CollectionViewExtensions.SortBy(query, collectionView.GroupDescriptions, collectionView.SortDescriptions));
 }
 /// <summary>
 /// Orders the query using the group and sort descriptions of the specified <paramref name="collectionView"/>.
 /// </summary>
 /// <typeparam name="TEntity">The generic type of the entity</typeparam>
 /// <param name="query">The query to sort</param>
 /// <param name="collectionView">The view containing the descriptions to sort by</param>
 /// <returns>A query ordered according to the specified <paramref name="collectionView"/></returns>
 public static EntityQuery <TEntity> SortBy <TEntity>(this EntityQuery <TEntity> query, ICollectionView collectionView) where TEntity : Entity
 {
     return(CollectionViewExtensions.SortBy(new QueryBuilder <TEntity>(), collectionView).ApplyTo(query));
 }