An open API service indexing awesome lists of open source software.

https://github.com/bzaar/enumerabletostream

Streams an IEnumerable<string>
https://github.com/bzaar/enumerabletostream

aspnetcore ienumerable stream streaming

Last synced: about 2 months ago
JSON representation

Streams an IEnumerable<string>

Awesome Lists containing this project

README

        

[![Build status](https://ci.appveyor.com/api/projects/status/q4c8f61tjwgv7cwu?svg=true)](https://ci.appveyor.com/project/morpher/enumerabletostream)

# EnumerableToStream [Nuget package][nuget]

Converts an ```IEnumerable``` to a ```Stream```:

```csharp
using EnumerableToStream;

IEnumerable 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()`.

[nuget]: https://www.nuget.org/packages/EnumerableToStream/

## Using in ASP.NET Core

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

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