I've implemented IAsyncEnumerable
to my HttpClient
requests where there is a pagination but I also need to GroupBy
them. So I've implemented code like below;
public class Item
{
public int Id {get; set;}
public string Name {get; set;}
}
public async IAsyncEnumerable<Item> GetItems()
{
while(hasMorePage)
{
// ... get paginated items
foreach(var item in paginatedItems)
{
yield return item;
}
}
}
// should find most repeated item(by Id) and count of them.
public async Task GroupItems()
{
IAsyncEnumerable<Item> items = GetItems();
//IAsyncGrouping
await foreach(var item in items.GroupBy(i => i.Id).
OrderByDescendingAwait(i => i.CountAsync()).
Take(10))
{
Console.WriteLine(item.Key.ToString() + (await item.CountAsync()).ToString());
}
}
This code works perfectly fine as I expected. But I would like to understand how GroupBy
works here, because of it should have all items to group by id is there something that I miss? or is there anything I can refactor for performance?