Skip to content

bzaar/EnumerableToStream

Repository files navigation

Build status

EnumerableToStream Nuget package

Converts an IEnumerable<string> to a Stream:

using EnumerableToStream;

IEnumerable<string> enumerable = new [] {"Hello, ", "world!"};

Stream stream = enumerable.ToStream();

Points of interest

  • 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 IEnumerable and IAsyncEnumerable. If you use the async version, you will need to call stream.ReadAsync() rather than Read().

Using in ASP.NET Core

Streaming query results to the client in a memory efficient manner:

public IActionResult Get()
{
    IEnumerable<string> lines = GetLines();
    Stream stream = lines.AddLineEndings().ToStream();
    return File(stream, "text/csv");
}