Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to create a constant value of type 'System.Object'. Only primitive types ('such as Int32, String, and Guid') are supported in this context

I'm using MVC and Entity Framework. I've created a class in my model folder with this code below. I keep getting the error message above with both queries below. I know there is a known issue on referencing non-scalar variables, but I'm not sure how to implement a workaround:

http://msdn.microsoft.com/en-us/library/bb896317.aspx#Y1442

private MovieLibraryDBEntities movieLibraryDBEntitiesContext;

public int getNumberOfEntriesReserved()
{
    return (from m in movieLibraryDBEntitiesContext.Movies
            where m.CheckedOut.Equals(1)
            select m).Count();

    //return movieLibraryDBEntitiesContext.Movies
    //  .Where(e => e.CheckedOut.Equals(1))
    //  .Select (e => e.Title).Count();
}
like image 533
user743183 Avatar asked May 07 '11 19:05

user743183


3 Answers

You cannot use m.CheckedOut.Equals(1) in linq-to-entities query. Use m.CheckedOut == 1 but CheckedOut must be integer.

like image 61
Ladislav Mrnka Avatar answered Oct 27 '22 19:10

Ladislav Mrnka


This is an older question. I had the same problem when trying to filter a nullable column using the IQueryable interface. I solved the problem by first checking to see if the object had a value and then checking the value.

widgets = widgets.Where(x => x.ID.HasValue.Equals(true) &&  x.ID.Value.Equals(widgetID));
like image 31
Tarzan Avatar answered Oct 27 '22 17:10

Tarzan


same issue using Any() i had to change my where clause to search on primitive types, for me int

so this

where order.User == user

becomes this

where order.User.UserId == user.UserId

There is a blog post explaining the quirk.

like image 26
Kieran Avatar answered Oct 27 '22 19:10

Kieran