Unable to construct a constant variable of type †System.Objectâ€TM in Entity Framework - Linq. There are only primitive kinds.

entity-framework expression-trees linq

Question

For a given type, property, and value, I have a way to create an expression for a linq query. As long as the type's property isn't nullable, this works well. Here is the illustration I'm using (http://www.marcuswhitworth.com/2009/12/dynamic-linq-with-expression-trees) I'm using the property's Equals method. But as it turns out, the Equals function for Nullable types actually accepts an Object as an argument rather than the Nullable type. I tried to conceal the null values using GetValueOrDefault, but EF doesn't allow that approach. The code that follows will fail simply by throwing an error:

decimal? testVal = new decimal?(2100);
        var test = (from i in context.Leases where i.AnnualRental.Equals(testVal) select i).ToList();

However, it will function properly if you use == rather than the Equals() method. However, I'm not sure how to change the code to use == rather than Equals(). Any recommendations would be much appreciated.

1
1
12/8/2010 5:46:25 PM

Popular Answer

When you say== , you produce aBinaryExpression using NodeTypeExpressionType.Equal . 14-zzz-zzz

When you say.Equals(x) , you produce aMethodCallExpression . TheMethodCallExpression There is a finite set of s that LinqToEntities can convert to SQL (for example none of your own undecorated methods are in that list).Nullable<T>.Equals(x) seems not to be on that list.

Do not speak.Equals(x) using LinqToEntities.

3
12/8/2010 6:31:31 PM


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