In Entity Framework, a constant variable of type 'System.Object' could not be created.

asp.net c# entity-framework

Question

I have a table named UserTenders having many-to-one relationship with aspnet_Membership table.
I am using EntityFramework 4.0 and when I try something like this, it errors.

var tenders = ctx.UserTenders
    .Where(tender => tender.HasAdminApproved.Equals(true))
    .ToList();

The error is

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.

This snippet below works.

var tenders = ctx.UserTenders.ToList();

What could be wrong in my code? Feel like I am missing something very trivial.

I would like to filter all those rows that have the bit field HasAdminApproved as true

1
21
7/19/2011 7:48:38 PM

Accepted Answer

Try replacing

.Where(tender => tender.HasAdminApproved.Equals(true))

With:

.Where(tender => tender.HasAdminApproved == true)

Or as previously suggested by @Ladislav Mrnka if your field is bool?

.Where(tender => tender.HasAdminApproved)
34
7/19/2011 8:00:55 PM

Popular Answer

@Ladislav told you the correct answer (.Where(tender => tender.HasAdminApproved)), but you might wonder why you get this message.

You're trying to call System.Boolean.Equals(Object obj). So you're boxing the constant true. And L2E, as the message says, has no support for a const of a non-primitive type like System.Object. Hence the error.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow