public static IEnumerable GetSortedQueryResultSet <TKey, TValue>(List <KeyValuePair <object, object> > list,
                                                                         PagingPredicate pagingPredicate, IterationType iterationType)
        {
            if (list.Count == 0)
            {
                return(new List <KeyValuePair <TKey, TValue> >());
            }

            var comparator = NewComparer(pagingPredicate.Comparer, iterationType);

            list.Sort(comparator);

            var nearestAnchorEntry = pagingPredicate.GetNearestAnchorEntry();
            var nearestPage        = nearestAnchorEntry.Key;
            var page     = pagingPredicate.Page;
            var pageSize = pagingPredicate.PageSize;
            var begin    = pageSize * (page - nearestPage - 1);
            var size     = list.Count;

            if (begin > size)
            {
                return(new List <KeyValuePair <TKey, TValue> >());
            }

            SetAnchor(list, pagingPredicate, nearestPage);

            var subList = list.GetRange(begin, Math.Min(pageSize, list.Count - begin));

            switch (iterationType)
            {
            case IterationType.Key:
                return(subList.Select(pair => pair.Key));

            case IterationType.Value:
                return(subList.Select(pair => pair.Value));

            case IterationType.Entry:
                return(subList.Select(pair => new KeyValuePair <TKey, TValue>((TKey)pair.Key, (TValue)pair.Value)));

            default:
                throw new ArgumentOutOfRangeException("iterationType", iterationType, null);
            }
        }
        public static SortedQueryResultSet <TKey, TValue> GetSortedQueryResultSet <TKey, TValue>(ISet <KeyValuePair <TKey, TValue> > list,
                                                                                                 PagingPredicate <TKey, TValue> pagingPredicate, IterationType iterationType)
        {
            if (list == null || list.Count == 0)
            {
                return(new SortedQueryResultSet <TKey, TValue>());
            }
            IComparer <KeyValuePair <TKey, TValue> > comparer   = SortingUtil.NewComparator(pagingPredicate.GetComparator(), iterationType);
            List <KeyValuePair <TKey, TValue> >      sortedList = new List <KeyValuePair <TKey, TValue> >();

            sortedList.AddRange(list);
            sortedList.Sort(comparer);

            KeyValuePair <int, KeyValuePair <TKey, TValue> > nearestAnchorEntry = pagingPredicate.GetNearestAnchorEntry();
            int nearestPage = nearestAnchorEntry.Key;
            int page        = pagingPredicate.GetPage();
            int pageSize    = pagingPredicate.GetPageSize();
            int begin       = pageSize * (page - nearestPage - 1);
            int size        = sortedList.Count;

            if (begin > size)
            {
                return(new SortedQueryResultSet <TKey, TValue>());
            }
            int end = begin + pageSize;

            if (end > size)
            {
                pageSize = size - begin;
            }

            SetAnchor(sortedList, pagingPredicate, nearestPage);
            List <KeyValuePair <TKey, TValue> > subList = sortedList.GetRange(begin, pageSize);

            return(new SortedQueryResultSet <TKey, TValue>(comparer, subList, iterationType));
        }