internal override void RefreshDistinctValuesForField(DataGridItemPropertyBase dataGridItemProperty)
        {
            if (dataGridItemProperty == null)
            {
                return;
            }

            if (dataGridItemProperty.CalculateDistinctValues == false)
            {
                return;
            }

            // List containing current column distinct values
            HashSet <object> currentColumnDistinctValues = new HashSet <object>();

            ReadOnlyObservableHashList readOnlyColumnDistinctValues = null;

            // If the key is not set in DistinctValues yet, do not calculate distinct values for this field
            if (!(( DistinctValuesDictionary )this.DistinctValues).InternalTryGetValue(dataGridItemProperty.Name, out readOnlyColumnDistinctValues))
            {
                return;
            }

            ObservableHashList columnDistinctValues = readOnlyColumnDistinctValues.InnerObservableHashList;

            // We use the DistinctValuesSortComparer if present, else the SortComparer for the DataGridItemProperty, else,
            // the Comparer used is the one of the base class.
            IComparer distinctValuesSortComparer = dataGridItemProperty.DistinctValuesSortComparer;

            if (distinctValuesSortComparer == null)
            {
                distinctValuesSortComparer = dataGridItemProperty.SortComparer;
            }

            using (columnDistinctValues.DeferINotifyCollectionChanged())
            {
                DataGridVirtualizingQueryableCollectionViewGroupRoot rootGroup = this.RootGroup as DataGridVirtualizingQueryableCollectionViewGroupRoot;

                Debug.Assert(rootGroup != null);

                object[] distinctValues = rootGroup.GetItemPropertyDistinctValues(dataGridItemProperty);

                foreach (object distinctValue in distinctValues)
                {
                    // Compute current value to be able to remove unused values
                    currentColumnDistinctValues.Add(distinctValue);
                }

                DataGridCollectionViewBase.RemoveUnusedDistinctValues(
                    distinctValuesSortComparer, currentColumnDistinctValues, columnDistinctValues, null);
            }
        }
        private IQueryable SortQueryable(IQueryable queryable, bool reverseSort)
        {
            DataGridVirtualizingCollectionViewBase parentCollectionView = this.GetCollectionView();

            Debug.Assert(parentCollectionView != null);

            SortDescriptionCollection explicitSortDescriptions = parentCollectionView.SortDescriptions;

            ListSortDirection directionToUseForImplicitSortDescriptions = ListSortDirection.Ascending;

            if (explicitSortDescriptions.Count > 0)
            {
                directionToUseForImplicitSortDescriptions = explicitSortDescriptions[explicitSortDescriptions.Count - 1].Direction;
            }

            SortDescriptionCollection implicitSortDescriptions = new SortDescriptionCollection();

            DataGridVirtualizingQueryableCollectionViewGroupRoot groupRoot =
                parentCollectionView.RootGroup as DataGridVirtualizingQueryableCollectionViewGroupRoot;

            Debug.Assert(groupRoot != null);

            string[] primaryKeys = groupRoot.PrimaryKeys;

            if (primaryKeys != null)
            {
                for (int i = 0; i < primaryKeys.Length; i++)
                {
                    string primaryKey = primaryKeys[i];

                    Debug.Assert(!string.IsNullOrEmpty(primaryKey));

                    implicitSortDescriptions.Add(new SortDescription(primaryKey, directionToUseForImplicitSortDescriptions));
                }
            }

            return(queryable.OrderBy(implicitSortDescriptions, explicitSortDescriptions, reverseSort));
        }