/// <summary> /// Iterate through all remaining rows in the next N result sets, mapping all rows to objects /// of the given type. Useful only if the next N result sets all represent the same type /// of object /// </summary> /// <typeparam name="T"></typeparam> /// <param name="results"></param> /// <param name="numResultSets">The number of result sets to consume</param> /// <param name="setup"></param> /// <returns></returns> public static IEnumerable <T> AsEnumerableNextSeveral <T>(this IDataResultsBase results, int numResultSets, Action <IMapCompilerSettings> setup = null) { Argument.NotNull(results, nameof(results)); if (numResultSets <= 0) { yield break; } var values = results.AsEnumerable <T>(setup); var currentSet = results.CurrentSet; foreach (var value in values) { if (currentSet != results.CurrentSet) { throw DataReaderException.EnumeratorBroken(); } yield return(value); } numResultSets--; while (numResultSets > 0) { // Make sure we're still where we think we are if (currentSet != results.CurrentSet) { throw DataReaderException.EnumeratorBroken(); } // Move to the next result set var hasMore = results.TryAdvanceToNextResultSet(); if (!hasMore) { yield break; } numResultSets--; // Make sure the next set is what we expect (no preemption) var nextSet = results.CurrentSet; if (nextSet != currentSet + 1) { throw DataReaderException.EnumeratorBroken(); } currentSet = nextSet; // Enumerate, checking before each value to make sure we are still on the correct // result set var nextValues = results.AsEnumerable <T>(setup); foreach (var value in nextValues) { if (currentSet != results.CurrentSet) { throw DataReaderException.EnumeratorBroken(); } yield return(value); } } }
public IEnumerator <T> GetEnumerator() { var attempt = Interlocked.Increment(ref _readAttempts); if (attempt > 1) { throw DataReaderException.ResultSetReadMoreThanOnce(); } if ((_context != null && _context.IsCompleted) || _reader.Reader.IsClosed) { throw DataReaderException.ReaderClosed(); } return(new ResultSetEnumerator(_results, _reader, _context, _map, _currentSet)); }
public IAsyncEnumerator <T> GetAsyncEnumerator(CancellationToken cancellationToken = new CancellationToken()) { var attempt = Interlocked.Increment(ref _readAttempts); if (attempt > 1) { throw DataReaderException.ResultSetReadMoreThanOnce(); } if (_context?.IsCompleted == true || _reader.Reader.IsClosed) { throw DataReaderException.ReaderClosed(); } return(new ResultSetEnumerator(_results, _reader, _context, _map, _currentSet)); }