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>
- Host: GitHub
- URL: https://github.com/bzaar/enumerabletostream
- Owner: bzaar
- License: mit
- Created: 2022-01-30T19:09:23.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-03-20T19:15:29.000Z (about 2 years ago)
- Last Synced: 2025-03-27T17:21:34.372Z (2 months ago)
- Topics: aspnetcore, ienumerable, stream, streaming
- Language: C#
- Homepage:
- Size: 45.9 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](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");
}
```