41
votes

I've seen lots of questions on this topic, but I haven't been able to sort through any of them that actually solve the issue I'm seeing. I have an activities entity that tracks which employee it is assigned to as well as which employee created the record and updated it. If I remove the `where a.AssignedEmployee == currentUser' line of code, I don't get the run time error below.

Unable to create a constant value of type 'DataModels.Employee'. Only primitive types or enumeration types are supported in this context.

CONTROLLER

var query = from a in db.Activities
            where a.AssignedEmployee == currentUser
            where a.IsComplete == false
            orderby a.DueDate
            select a;
return View(query.ToList());

VIEW

@model IEnumerable<Data.DataModels.Activity>
..........
3
Is the == performing reference equality which isn't supported in this scenario? Note: asking if ref equality not supportedJaredPar
@JaredPar you're right... Idiot of the day award goes to me. I tried 18 different ways to Sunday to write this query and for some reason using the ID property on that object wasn't one of them. I'll blame it on the lack of Starbucks today.RSolberg
coffee is your friend ;)JaredPar

3 Answers

65
votes

My guess is that error indicates that EF cannot translate the equality operator for Employee to SQL (regardless of whether you're assuming referential equality or an overridden == operator). Assuming the Employee class has a unique identifier try:

var query = from a in db.Activities
            where a.AssignedEmployeeId == currentUser.Id
            where a.IsComplete == false
            orderby a.DueDate
            select a;
return View(query.ToList());
8
votes

It doesn't like the fact that you're trying to convert a whole object equality into a database query. You can only do entity framework queries using constant values, much like how you would do SQL queries. The way to solve this would be to compare the IDs to see if the AssignedEmployee's ID is the same as the current user's ID in the employee table.

As a side note, if the currentUser object that you're tracking isn't of the Employee type you might want to consider caching the corresponding Employee record of that user to better be able to reference it in later queries. That would be a lot better than trying to go through that table constantly. (Again this would only affect you if it is, in fact, in a different table)

5
votes

The problem with using == and obj.Equals is that Entity Framework doesn't know how to translate that method call into SQL---even if you overload those two methods into something that would translate into SQL. What you can do to fix this shortcoming in Entity Framework is to create a method that returns an Expression Tree that does the more complex equality checking you are wanting to do.

For example, let's say we have the following class

public class Person {
    public string Firstname { get; set; } 
    public string Lastname  { get; set; }
}

In order to return a custom equality operation that Entity Framework can understand, add the following method to the Person class:

public static Expression<Func<Person, bool>> EqualsExpressionTree(  Person rhs )
{
    return ( lhs ) => string.Equals( lhs.Firstname, rhs.Firstname ) &&
                      string.Equals( lhs.Lastname, rhs.Lastname );
}

In your LINQ queries, you can leverage your custom equality code like so:

Person anotherPerson = new Person { Firstname = "John", Lastname = "Doe" }
personCont.Where( Person.EqualsExpressionTree(anotherPerson) );
//...
if ( personCont.Any( Person.EqualsExpressionTree(anotherPerson)) ) {
//...

Furthermore, the EqualsExpressionTree method can be rewritten to call a static Equals method, allowing you to leverage your custom equality logic. However, in all things, remember that your code must translate to a SQL expression, since we are, after all, calling to a database and not accessing stuff from memory.