Hi i am trying to use DefaultIfEmpty() function on IQueryable and it’s throwing an exception “Unsupported overload used for query operator ‘DefaultIfEmpty’.”
this is my code:
Dinner defaultDinner = db.Dinners.Where(d => d.DinnerID == 5).Single();
Dinner blah;
IQueryable<Dinner> bla = db.Dinners.Where(d => d.DinnerID == id)
.DefaultIfEmpty(defaultDinner);
blah = bla.First();
return blah;
I found a different way to do it without DefaultIfEmpty but i still want to know how to solve this… here is the first part of the exception:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedException: Unsupported overload used for query operator ‘DefaultIfEmpty’.
It seems pretty self-explanatory to me:
Sounds like your LINQ provider (which you haven’t specified) doesn’t support the overload of
DefaultIfEmptywhich takes a default value.The simplest alternative is probably to use:
Note that this approach avoids fetching the “default” dinner unless it’s required, so it’s more efficient too.
(If there should only be a single result for any ID, you should probably use
SingleOrDefaultinstead ofFirstOrDefaultby the way. It’s more logical that way.)