Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I use AsAsyncEnumerable in Linq?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading