I'm trying to have an Expression resolved by LINQ/SQL Server, but it seems it can't handle the expression and just skips it altogether (it works on an in-memory dataset though).
public static Func<TSource, bool> WhereNotNullClause<TSource>(string propertyName)
{
var type = typeof(TSource);
var expression = Expression.Parameter(type, "p");
var propertyNameReference = Expression.Property(expression, propertyName);
var propertyValueReference = Expression.Constant(null);
return Expression.Lambda<Func<TSource, bool>>(
Expression.NotEqual(propertyNameReference, propertyValueReference),
new[] { expression }).Compile();
}
Called as follows:
var whereNotNullSelector = Expressions.WhereNotNullClause<ContactItem>("property");
var contactItems = context.ContactItems.Where(whereNotNullSelector).ToList();
The SQL generated does not include the where clause, so the where seems to be executed after the query resolves. What more things do I need to look after before I can get this to resolve properlY?