I've been working with the Entity Framework lately, and I tried to do a LINQ query that returned a record/object with an id that was equal to a value represented by an enum type.  I ended up getting the following error: "Unable to create a constant value of type 'Closure.type'.  Only primitive types ('such as Int32, String, and Guid') are supported in this context."
The code looked similar to below:

public void Test(MyEnum mEnum)

{ var s = (from mt in MyContext.MyTable where (mt.ID == (int) mEnum) select mt).FirstOrDefault(); }

Even though we are casting mEnum before evaluating it, Linq to Entities has a problem. 

 

The solution is to create an int variable prior to running the query as shown below:

public void Test(MyEnum mEnum)
{
    int myEnumValue = (int) mEnum;
    var s = (from mt in MyContext.MyTable where (mt.ID == myEnumValue) select mt).FirstOrDefault();
}
 
Based on this blog post I found: Matt Hidinger's Blog Post I found out why you cannot do high level comparisons (objects) or even functions that return primitives, so I guess we can extend this to say the casting of an enum doesn't work either!