System.Linq is Ambiguous

I’m using net core 3.1 webapi and implement couchbase (in Net Standard library project) but getting errors all of DbSet.Where statements (by the way EF Code First ). Error is between System.Linq.Queryable and System.Linq. AsyncEnumerable. I think the two libraries are overlapping.

The call is ambiguous between the following methods or properties: 
'System.Linq.AsyncEnumerable.Where<TSource>(System.Collections.Generic.IAsyncEnumerable<TSource>, System.Func<TSource, bool>)' 
and 'System.Linq.Queryable.Where   .....

Hi @splat1985,

Usually this is a matter of namespaces / using statements / aliases. Would you please post the code that’s causing this compiler error (along with the “usings” namespaces that you have in that file)?

Hi Matthew,

Thanks for answer. I sending the codes. When I add Couchbase Net Client reference in Nuget get this error.

Error on this line: 53 in “Where” method.
item.companyName = context.CronDatas.Where(x => x.companyid == cmp_id).Select(x => x.companyname).FirstOrDefault();

ProfileController.zip (913 Bytes)

I don’t see anything in this code that uses Couchbase. There are no using statements that mention Couchbase.

The code you pointed out is related to Entity Framework, which is probably not using Couchbase. Without seeing all the SerdarProject code, I couldn’t begin to tell you what the problem is. I do know that IAsyncEnumerable is used in Couchbase SDK 3.x, but based on what I’m seeing, I don’t know why that would cause this conflict.

I wonder if it might be related to the transient dependency on System.Linq.Async, which does implement extension methods for IAsyncEnumerable. These could be in conflict with other extension methods added by Entity Framework. I’ve seen some similar reports, this could be another similar case: https://github.com/dotnet/efcore/issues/18220

@splat1985

I’ve looked into this a bit, and I’m able to reproduce what you’re describing, or at least something close to it, using EFCore 3.1.8.

The DbSet class implements more than one interface, both IQueryable and IAsyncEnumerable. So when accessing a property of type DbSet directly, it will look for extension methods that extend both of those interfaces to find a matching extension method. In a pure EF Core installation, there are no extensions available for IAsyncEnumerable. However, the System.Linq.Async library, published by the Rx.NET team, adds LINQ extensions to IAsyncEnumerable. This library is included as a dependency of the Couchbase SDK so we can use some of it’s features and offer them to the SDK’s users, though you could get this dependency from many other places.

Once the System.Linq.Async package is included in your project, both interfaces now have an implementation for Where, and C# can’t tell which is the best to choose. In fact, for your use case the IQueryable extension is the right choice, but the C# compiler can’t know that.

The workaround is to use .AsQueryable() which will typecast the DbSet to IQueryable and remove the ambiguity from the C# compiler. In fact, this is documented on that method in EF Core:

This is a convenience method to help with disambiguation of extension methods in the same namespace that extend both interfaces.

Hopefully this helps!

1 Like

Hi @btburnett3

Thanks for your answer. I guess that’s exactly my problem.

This library is included as a dependency of the Couchbase SDK

and I tried your solution and the error was gone. This seems to fix my problem. I will just need to add “.AsQueryable” before “.where” in the whole project.

Thank you all for your help and effort.

1 Like