Where statement on a IAsyncEnumerable

If you would like to use a where statement on an IAsyncEnumerable like

IAsyncEnumerable<company> companyAsyncEnum = _dataRepository.GetCampingsAsync();
var asyncFiltered = companyAsyncEnum.Where(c => c.Website != null && c.Website.Uri != null);

The following extension method will help you
public static class AsyncFilterExtensions
{
    public static async IAsyncEnumerable Where<C>(this IAsyncEnumerable asyncEnum, Funcbool> filter)
   {
        await foreach (var item in asyncEnum)
    {
        if (filter(item))
                      yield return item;
         }
  }
}

Comments

Popular posts from this blog

System.Net.Http dll version problems

SharePoint Survey Back Button

Azure table storage error: One of the request inputs is not valid