Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

623
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda