iasyncenumerable

What's the difference between returning AsyncEnumerable with EnumeratorCancellation or looping WithCancellation

时光毁灭记忆、已成空白 提交于 2021-02-07 05:53:46
问题 I have the following method that reads a csv document from a http stream public async IAsyncEnumerable<Line> GetLines([EnumeratorCancellation] CancellationToken cancellationToken) { HttpResponseMessage response = GetResponse(); using var responseStream = await response.Content.ReadAsStreamAsync(); using var streamReader = new StreamReader(responseStream); using var csvReader = new CsvReader(streamReader); while (!cancellationToken.IsCancellationRequested && await csvReader.ReadAsync()) {

Difference between “ToListAsync()” and “AsAsyncEnumerable().ToList()”

早过忘川 提交于 2021-01-03 07:10:21
问题 Function need to return Task<List<Record>> Following both options are returning Task<List<Record>> , which one is more efficient? Is there any standard way here? Option 1 : Task<List<Record>> GetRecords() { return DbContext.Set<Record>.Where(predicate).ToListAsync(); } Option 2: Task<List<Record>> GetRecords() { return DbContext.Set<Record>.Where(predicate).AsAsyncEnumerable().ToList(); } 回答1: Go for option 1 ToListAsync as the source code of AsAsyncEnumerable explicitly mentions This is an

Iterating an IAsyncEnumerable in a function returning an IAsyncEnumerable with cancellation

◇◆丶佛笑我妖孽 提交于 2020-12-09 05:10:39
问题 As the title says, I have to following function: public async IAsyncEnumerable<Job> GetByPipeline(int pipelineId, [EnumeratorCancellation] CancellationToken cancellationToken = default) { await foreach (var job in context.Jobs.Where(job => job.Pipeline.Id == pipelineId) .AsAsyncEnumerable() .WithCancellation(cancellationToken) .ConfigureAwait(false)) { yield return job; } } I have trouble wrapping my head around where the cancellation token is going, and a nagging feeling that I am using it

Linq methods for IAsyncEnumerable

≡放荡痞女 提交于 2020-08-26 19:48:29
问题 When working with an IEnumerable<T> there are the build-in extension methods from the System.Linq namespace such as Skip , Where and Select to work with. When Microsoft added IAsyncEnumerable in C#8 did they also add new Linq methods to support this? I could of course implement these methods myself, or maybe find some package which does that, but I'd prefer to use a language-standard method if it exists. 回答1: There is, in the System.Linq.Async namespace from the System.Reactive package. If