Search code examples
c#asynchronoustask-parallel-libraryienumerableiasyncenumerable

Reuse a IAsyncEnumerable instance without having to iterate again


I am currently writing a method like this:

public async Task<UserModel> GetUserByUserIdAsync(string userId)
{
    IQueryable<UserEntity> usersQuery = BuildQueryable(userId);

    bool any = await usersQuery.ExecuteQuery().AnyAsync();
    
    if (!any) return null; // wanna do other logic in the future

    return (await usersQuery.ExecuteQuery().SingleAsync()).ToUserModel();
}

As you can see, I am calling the await usersQuery.ExecuteQuery() twice, and ExecuteQuery() is a method which iterates my database and could be considered an expensive operation. Is there any way I could save my IAsyncEnumerable<T> like I normally would with IEnumerable<T> and re-use it throughout my code?

I thought about using ToListAsync() on it, but I am unsure whether that is considered good practice or not. I've also read that I could return a Task<IAsyncEnumerable<T>> and do something with that maybe. What is the best way to handle this? I'd like to implement the most efficient solution.


Solution

  • Why not simply use SingleOrDefaultAsync? Assuming your entity is a reference type you can get your single item, check if it is null to handle the empty-case. Another alternative is always to convert the enumerable to a list. Then you can iterate over it however many times you want to.