[Solved] Convert non-async Linq Extension method to Async [closed]


This is an XY problem. What you’re trying to achieve isn’t the same thing as what your question is asking. Your desired syntax wouldn’t work, even if you made this method async:

var courses = await ids.ToChunks(1000)
                 .Select(chunk => Courses.Where(c => chunk.Contains(c.CourseID)))
                 .SelectMany(x => x).ToListAsync();

And the result you really want is just as easily accomplished without making this method async:

var courses = new List<Course>();
foreach(var chunk in ids.ToChunks(1000))
{
    courses.AddRange(await Courses.Where(c => chunk.Contains(c.CourseID)).ToListAsync());
}

Note: As Syatoslav pointed out, any attempt to get better performance via concurrency will be thwarted by the fact that DbContext won’t let you perform multiple concurrent async calls. So you really won’t get much advantage to doing this asynchronously anyway. And in some cases, you might find it to be much slower.

12

solved Convert non-async Linq Extension method to Async [closed]