Ambiguous Invocation IQueryable or IEnumerable

Got it. People don't seem to like this answer, but it solved the problem I described in the question.

db.Set.AsQueryable().Where(...

I had this problem - In my case the model was in a separate project from the calling code. The calling code did not have a reference to EF. Adding reference to EF in calling project fixed it for me.


The problem that many people will have is because IQueryable inherits from IEnumerable, and they both have their own implementation of the various LINQ statements.

Rhys's answer covers another situation than this. If you implement what I say below, and still receive the error, then you are probably running into his situation.

This answer shows the differences between IEnumerable and IQueryable. This article explains what .AsQueryable() is good for.

In the case of this particular question, .AsQueryable() is not the right way to do this because the purpose is primarily for conversion of IEnumerables into pseudo-queryables. The proper way to do this is to be explicit to the compiler what your object is.

IQueryable<whateverSet> dbSet = db.Set;
var result = dbSet.Where(x => x.Id == num).Select(whatever);

This is telling the compiler what dbSet is explicitly rather than trying to execute a cast statement as in the case of .AsQueryable().

The addition of var in C# 3.0 was fantastic for people coming from loosely typed languages like PHP, however it fools folks into thinking that the compiler will always be able to just "figure it out". There are plenty of times this isn't the case, and casting is NOT the solution.


This problem is normally caused by a failure in type inference from in the expression provided to the Where statement. As was mentioned in the comments above it is 100% caused by the assignment operator in the lambda returning an int instead of a bool.

To be clear - where you have

var result = db.Set.Where(x => x.Id = num).Select(whatever);

You should have

var result = db.Set.Where(x => x.Id == num).Select(whatever);

Another good (and more common) example of this is something like this

public class Elem 
{
    public bool IsSomething {get;set;}                    
    public bool? IsSomethingElse {get;set;}
}

Then if you do the following query, which looks very reasonable at fist glance, it will fail to compile with the rather puzzling error of "abiguous invocation"

IQueryable<Elem> queryable = GetQueryable();
var result = queryable.Where(e => e.IsSomething && e.IsSomethingElse).ToList();

If you weren't writing this statement inside a lambda then you would get a more meaningful error of

"Cannot apply operator '&&' to operands of type 'System.Nullable<bool>' and 'bool'"

Which would immediately tell you that you are failing to return a boolean.