The expression <Func <T, bool >> adds an unwanted transformation when created in the general method
I have a function to generate an expression that will be used in the linq Where clause.
public static Expression<Func<T,bool>> GetWhereCondition<T>() where T : IActive { return x => x.Active; } (note IActive defines the "Active" property)
There are other related functions, and the idea is that I can nest the required conditions in a Generic class to manage business rules, etc.
The problem is that when I run this, the returned expression contains lamda (seen from the debugger):
x => Convert(x).Active Which, of course, is rejected by linq: 'LINQ to Entities only supports listing of Entity Data Model primitive data types.'
So my question is ...
How to prevent this behavior. There is no need for conversion, and this is clearly undesirable. Is it possible to prevent this?
Well, assuming that this is only necessary for working with classes (the conversion is for types of box values), you can add a class constraint:
public static Expression<Func<T, bool>> GetWhereCondition<T>() where T : class, IActive { return x => x.Active; } ... and the transformation goes away.
Try the following:
public static Expression<Func<T, bool>> GetWhereCondition<T>() where T : IActive { return x => x.Active; }