Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

631
Visualizações
How to convert CosmosDB FeedIterator result to IAsyncEnumerable or IEnumerable?

It's a bit tricky to get data from CosmosDb FeedIterator converted into IEnumerable in a simple reusable way without having to write loops and iteration all over the place.

The official Microsoft example which is not as neat as I like it looks like this:

using (FeedIterator setIterator = container.GetItemLinqQueryable<Book>()
                     .Where(b => b.Title == "War and Peace")
                     .ToFeedIterator())
{                   
    //Asynchronous query execution
    while (setIterator.HasMoreResults)
    {
        foreach(var item in await setIterator.ReadNextAsync())
        {
            Console.WriteLine(item.Price); 
        }
    }
}

I'd like to have a reusable oneliner and didn't find one, so I wrote one.

Especially when it's to be used in a .NET API so I wrote an extension method to convert the FeedIterator to IAsyncEnumerable that you can use the

(In C# 8 you can return IAsyncEnumerable from your API but if you need compatibility with netstandard2.0 as I do you can convert the IAsyncEnumerable to a regular IEnumerable)

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

This is how I solved it:

Extension method:

public static class CosmosDbExtensions
{
    /// <summary>
    /// Convert a feed iterator to IAsyncEnumerable
    /// </summary>
    /// <typeparam name="TModel"></typeparam>
    /// <param name="setIterator"></param>
    /// <returns></returns>
    public static async IAsyncEnumerable<TModel> ToAsyncEnumerable<TModel>(this FeedIterator<TModel> setIterator)
    {
        while (setIterator.HasMoreResults)
            foreach (var item in await setIterator.ReadNextAsync())
            {
                yield return item;
            }
    }
}

To return IAsyncEnumerable:

 public virtual IAsyncEnumerable<TModel> Query()
    {
        Container container = GetContainer(); // Your code to get container 

        return  container.GetItemLinqQueryable<TModel>().ToFeedIterator().ToAsyncEnumerable();
    }

To return IEnumerable:

public virtual async Task<IEnumerable<TModel>> Query()
{

    Container container = GetContainer(); // Your code to get container 

    using (var feedIterator = container.GetItemLinqQueryable<TModel>().ToFeedIterator())
    {
        return await feedIterator.ToAsyncEnumerable().ToListAsync();
    }
}

(More about .ToListAsync() from the System.Linq.Async package in this thread)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda