/// <summary>
        /// Initializes a new instance of the <see cref="SortCollectionManager"/> class.
        /// </summary>
        /// <param name="sourceCollection">The collection of <see cref="SortDescriptor"/>s to manage</param>
        /// <param name="descriptionCollection">The collection of <see cref="SortDescription"/>s to synchronize with the <paramref name="sourceCollection"/></param>
        /// <param name="expressionCache">The cache with entries for the <see cref="SortDescriptor"/>s</param>
        /// <param name="validationAction">The callback for validating items that are added or changed</param>
        public SortCollectionManager(SortDescriptorCollection sourceCollection, SortDescriptionCollection descriptionCollection, ExpressionCache expressionCache, Action <SortDescriptor> validationAction)
        {
            if (sourceCollection == null)
            {
                throw new ArgumentNullException("sourceCollection");
            }
            if (descriptionCollection == null)
            {
                throw new ArgumentNullException("descriptionCollection");
            }
            if (expressionCache == null)
            {
                throw new ArgumentNullException("expressionCache");
            }
            if (validationAction == null)
            {
                throw new ArgumentNullException("validationAction");
            }

            this._sourceCollection            = sourceCollection;
            this._descriptionCollection       = descriptionCollection;
            this.ExpressionCache              = expressionCache;
            this.ValidationAction             = (item) => validationAction((SortDescriptor)item);
            this.AsINotifyPropertyChangedFunc = (item) => ((SortDescriptor)item).Notifier;

            ((INotifyCollectionChanged)descriptionCollection).CollectionChanged += this.HandleDescriptionCollectionChanged;

            this.AddCollection(sourceCollection);
        }
        /// <summary>
        /// Determines whether the <paramref name="sortDescriptions"/> are equivalent to the <paramref name="sortDescriptors"/>.
        /// </summary>
        /// <param name="sortDescriptions">The descriptions to compare</param>
        /// <param name="sortDescriptors">The descriptors to compare</param>
        /// <returns><c>true</c> if the collections are equivalent</returns>
        public static bool AreEquivalent(SortDescriptionCollection sortDescriptions, SortDescriptorCollection sortDescriptors)
        {
            Debug.Assert((sortDescriptions != null) && (sortDescriptors != null), "Both should be non-null.");

            if (sortDescriptions.Count != sortDescriptors.Count)
            {
                return(false);
            }

            for (int i = 0, count = sortDescriptions.Count; i < count; i++)
            {
                if (!SortCollectionManager.AreEquivalent(sortDescriptions[i], sortDescriptors[i]))
                {
                    return(false);
                }
            }

            return(true);
        }
 /// <summary>
 /// Resets the <paramref name="sortDescriptors"/> collection to match the <paramref name="sortDescriptions"/> collection.
 /// </summary>
 /// <param name="sortDescriptions">The collection to match</param>
 /// <param name="sortDescriptors">The collection to reset</param>
 private static void ResetToSortDescriptions(SortDescriptionCollection sortDescriptions, SortDescriptorCollection sortDescriptors)
 {
     sortDescriptors.Clear();
     foreach (SortDescription description in sortDescriptions)
     {
         sortDescriptors.Add(SortCollectionManager.GetDescriptorFromDescription(description));
     }
 }
 /// <summary>
 /// Resets the <paramref name="sortDescriptions"/> collection to match the <paramref name="sortDescriptors"/> collection.
 /// </summary>
 /// <param name="sortDescriptions">The collection to reset</param>
 /// <param name="sortDescriptors">The collection to match</param>
 private static void ResetToSortDescriptors(SortDescriptionCollection sortDescriptions, SortDescriptorCollection sortDescriptors)
 {
     sortDescriptions.Clear();
     foreach (SortDescriptor descriptor in sortDescriptors)
     {
         SortDescription?description = SortCollectionManager.GetDescriptionFromDescriptor(descriptor);
         if (description.HasValue)
         {
             sortDescriptions.Add(description.Value);
         }
     }
 }
Exemple #5
0
        /// <summary>
        /// Composes an <see cref="EntityQuery" /> for sorting and grouping purposes.
        /// </summary>
        /// <param name="source">The queryable source.</param>
        /// <param name="groupDescriptors">The group descriptors.</param>
        /// <param name="sortDescriptors">The sort descriptors.</param>
        /// <param name="expressionCache">Cache for storing built expressions</param>
        /// <returns>The composed <see cref="EntityQuery" />.</returns>
        public static EntityQuery OrderBy(
            EntityQuery source,
            GroupDescriptorCollection groupDescriptors,
            SortDescriptorCollection sortDescriptors,
            ExpressionCache expressionCache)
        {
            Debug.Assert(source != null, "Unexpected null source");
            Debug.Assert(sortDescriptors != null, "Unexpected null sortDescriptors");
            Debug.Assert(groupDescriptors != null, "Unexpected null groupDescriptors");

            bool hasOrderBy = false;

            // check the GroupDescriptors first
            foreach (GroupDescriptor groupDescriptor in groupDescriptors)
            {
                if (groupDescriptor != null && groupDescriptor.PropertyPath != null)
                {
                    Debug.Assert(expressionCache.ContainsKey(groupDescriptor), "There should be a cached group expression");

                    // check to see if we sort by the same parameter in desc order
                    bool sortAsc = true;
                    foreach (SortDescriptor sortDescriptor in sortDescriptors)
                    {
                        if (sortDescriptor != null)
                        {
                            string sortDescriptorPropertyPath  = sortDescriptor.PropertyPath;
                            string groupDescriptorPropertyPath = groupDescriptor.PropertyPath;

                            if (sortDescriptorPropertyPath != null &&
                                sortDescriptorPropertyPath.Equals(groupDescriptorPropertyPath))
                            {
                                if (sortDescriptor.Direction == ListSortDirection.Descending)
                                {
                                    sortAsc = false;
                                }

                                break;
                            }
                        }
                    }

                    string orderMethodName = (!hasOrderBy ? "OrderBy" : "ThenBy");
                    if (!sortAsc)
                    {
                        orderMethodName += "Descending";
                    }

                    source     = OrderBy(source, orderMethodName, expressionCache[groupDescriptor]);
                    hasOrderBy = true;
                }
            }

            // then check the SortDescriptors
            foreach (SortDescriptor sortDescriptor in sortDescriptors)
            {
                if (sortDescriptor != null)
                {
                    Debug.Assert(expressionCache.ContainsKey(sortDescriptor), "There should be a cached sort expression");

                    string orderMethodName = (!hasOrderBy ? "OrderBy" : "ThenBy");
                    if (sortDescriptor.Direction == ListSortDirection.Descending)
                    {
                        orderMethodName += "Descending";
                    }

                    source     = OrderBy(source, orderMethodName, expressionCache[sortDescriptor]);
                    hasOrderBy = true;
                }
            }

            return(source);
        }