public async Task <bool> MoveNext(CancellationToken cancellationToken)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (_groupsEnumerator == null)
                    {
                        var groups = new Dictionary <TKey, Grouping <TKey, TElement> >();

                        using (var sourceEnumerator = _groupByAsyncEnumerable._source.GetEnumerator())
                        {
                            while (await sourceEnumerator.MoveNext(cancellationToken))
                            {
                                var key     = _groupByAsyncEnumerable._keySelector(sourceEnumerator.Current);
                                var element = _groupByAsyncEnumerable._elementSelector(sourceEnumerator.Current);

                                Grouping <TKey, TElement> grouping;
                                if (!groups.TryGetValue(key, out grouping))
                                {
                                    groups.Add(key, grouping = new Grouping <TKey, TElement>(key));
                                }

                                grouping.Add(element);
                            }
                        }

                        _groupsEnumerator = groups.Values.GetEnumerator();
                    }

                    return(_groupsEnumerator.MoveNext());
                }
Esempio n. 2
0
                public async Task <bool> MoveNext(CancellationToken cancellationToken)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    if (_sourceEnumerator == null)
                    {
                        _sourceEnumerator = _groupByAsyncEnumerable._source.GetEnumerator();
                        _hasNext          = await _sourceEnumerator.MoveNext(cancellationToken);
                    }

                    if (_hasNext)
                    {
                        var currentKey = _groupByAsyncEnumerable._keySelector(_sourceEnumerator.Current);
                        var element    = _groupByAsyncEnumerable._elementSelector(_sourceEnumerator.Current);
                        var grouping   = new Grouping <TKey, TElement>(currentKey)
                        {
                            element
                        };

                        while (true)
                        {
                            _hasNext = await _sourceEnumerator.MoveNext(cancellationToken);

                            if (!_hasNext)
                            {
                                break;
                            }

                            if (!_comparer.Equals(
                                    currentKey,
                                    _groupByAsyncEnumerable._keySelector(_sourceEnumerator.Current)))
                            {
                                break;
                            }

                            grouping.Add(_groupByAsyncEnumerable._elementSelector(_sourceEnumerator.Current));
                        }

                        Current = grouping;

                        return(true);
                    }

                    return(false);
                }