How can I use AsAsyncEnumerable in Linq?

Advertisements

I have this method:

    public async Task<List<string>> FindUsedComments(List<string> commentIds)
    {
        if (!commentIds.Any())
            return new List<string>();

        var usedCommentIds = GetNoTraking.Select(x => x.OrginalCommentId).AsEnumerable().Intersect(commentIds).ToList();

        return usedCommentIds;
    }

and it works fine! but if i change AsEnumerable to AsAsyncEnumerable it`s show me this error:

'IAsyncEnumerable<string>' does not contain a definition for 'Intersect' and the best extension method overload 'ParallelEnumerable.Intersect<string>(ParallelQuery<string>, IEnumerable<string>)' requires a receiver of type 'System.Linq.ParallelQuery<string>'

how can is use AsAsyncEnumerable instand of AsEnumerable in my method?

>Solution :

You should install the NuGet package System.Linq.Async which gives you asynchronous LINQ methods. You’ll need using System.Linq; as normal.

Usage:

IAsyncEnumerable<int> a = getEnumerableA(); // however you get this
IAsyncEnumerable<int> b = getEnumerableB(); // however you get this

IAsyncEnumerable<int> c = a.Intersect(b);

Leave a ReplyCancel reply