EnumerableToStream Nuget package
Converts an IEnumerable<string> to a Stream:
usingEnumerableToStream;IEnumerable<string>enumerable=new[]{"Hello, ","world!"};Streamstream=enumerable.ToStream();- The enumerable is evaluated lazily as the stream is read.
- The enumerable is properly disposed of when the stream is closed.
- ToStream() does zero allocations on .NET Standard 2.1 compatible runtimes.
- ToStream() supports encodings:
enumerable.ToStream(Encoding.UTF8); - ToStream() accepts both
IEnumerableandIAsyncEnumerable. If you use the async version, you will need to callstream.ReadAsync()rather thanRead().
Streaming query results to the client in a memory efficient manner:
publicIActionResultGet(){IEnumerable<string>lines=GetLines();Streamstream=lines.AddLineEndings().ToStream();returnFile(stream,"text/csv");}