I can’t seem to figure out how to compare boolean values in a C# lambda expression for EF4. I’ve tried:
cl.Where(c => c.Received == false);
and this:
cl.Where(c => !c.Received);
and this:
cl.Where(c => c.Received.Equals(false));
but I keep getting this error:
Exception Details: System.NotSupportedException: Unable to create a constant value
of type 'System.Object'. Only primitive types ('such as Int32, String, and Guid')
are supported in this context.
After spending a good amount of time researching this I’m still missing something. I’m fairly new to Lambdas so pointers would be appreciated.
Edit2: more code re:comment
int bar = 42;
var cl = db.foo.Where(c => c.baz.Equals(bar));
//codez (just an if statement)
cl.Where(c => c.Received == false).OrderByDescending(c => c.dateAdded);
That’s it. Even if I remove the orderby it still doesn’t work
Edit3:
Solution:
int bar = 42;
var cl = db.foo.Where(c => c.baz == bar);
cl.Where(c => c.Received == false).OrderByDescending(c => c.dateAdded);
The issue is most likely in the
c.baz.Equals(bar)line. If you change it toyou should see the exception thrown on that line, because you force evaluation of the
IQueryable<T>.Instead of comparing objects, you should compare their IDs, like this:
(edited to reflect the conversation in the comments and changes to the OP)