/// <summary> /// Initializes a new instance of the <see cref="GroupCollectionManager"/> class. /// </summary> /// <param name="sourceCollection">The collection of <see cref="GroupDescriptor"/>s to manage</param> /// <param name="descriptionCollection">The collection of <see cref="GroupDescription"/>s to synchronize with the <paramref name="sourceCollection"/></param> /// <param name="expressionCache">The cache with entries for the <see cref="GroupDescriptor"/>s</param> /// <param name="validationAction">The callback for validating items that are added or changed</param> public GroupCollectionManager(GroupDescriptorCollection sourceCollection, ObservableCollection <GroupDescription> descriptionCollection, ExpressionCache expressionCache, Action <GroupDescriptor> 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._groupValidationAction = validationAction; this.ValidationAction = this.Validate; this.AsINotifyPropertyChangedFunc = this.AsINotifyPropertyChanged; this.AddCollection(sourceCollection); this.AddCollection(descriptionCollection); }
/// <summary> /// Determines whether the <paramref name="groupDescriptions"/> are equivalent to the <paramref name="groupDescriptors"/>. /// </summary> /// <param name="groupDescriptions">The descriptions to compare</param> /// <param name="groupDescriptors">The descriptors to compare</param> /// <returns><c>true</c> if the collections are equivalent</returns> private static bool AreEquivalent(ObservableCollection <GroupDescription> groupDescriptions, GroupDescriptorCollection groupDescriptors) { Debug.Assert((groupDescriptions != null) && (groupDescriptors != null), "Both should be non-null."); if (groupDescriptions.Count != groupDescriptors.Count) { return(false); } for (int i = 0, count = groupDescriptions.Count; i < count; i++) { if (!GroupCollectionManager.AreEquivalent(groupDescriptions[i], groupDescriptors[i])) { return(false); } } return(true); }
/// <summary> /// Resets the <paramref name="groupDescriptors"/> collection to match the <paramref name="groupDescriptions"/> collection. /// </summary> /// <param name="groupDescriptions">The collection to match</param> /// <param name="groupDescriptors">The collection to reset</param> private static void ResetToGroupDescriptions(ObservableCollection <GroupDescription> groupDescriptions, GroupDescriptorCollection groupDescriptors) { groupDescriptors.Clear(); foreach (GroupDescription description in groupDescriptions) { groupDescriptors.Add(GroupCollectionManager.GetDescriptorFromDescription(description)); } }
/// <summary> /// Resets the <paramref name="groupDescriptions"/> collection to match the <paramref name="groupDescriptors"/> collection. /// </summary> /// <param name="groupDescriptions">The collection to reset</param> /// <param name="groupDescriptors">The collection to match</param> private static void ResetToGroupDescriptors(ObservableCollection <GroupDescription> groupDescriptions, GroupDescriptorCollection groupDescriptors) { groupDescriptions.Clear(); foreach (GroupDescriptor descriptor in groupDescriptors) { GroupDescription description = GroupCollectionManager.GetDescriptionFromDescriptor(descriptor); if (description != null) { groupDescriptions.Add(description); } } }
/// <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); }