您的方法返回一个
Func
Expression
Enumerable.Where
而不是
Queryable.Where
.Compile
:
public static Expression<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 });
}
一些额外的阅读
here
关于为什么你
而不是
Func公司
。将表达式视为数据而不是实际的委托,这意味着Linq to SQL可以将表达式树转换为SQL,而Func本质上是一个黑匣子。