【发布时间】:2017-07-13 10:09:21
【问题描述】:
我正在尝试让 LINQ/SQL Server 解析一个表达式,但它似乎无法处理该表达式并完全跳过它(尽管它适用于内存数据集)。
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();
}
调用如下:
var whereNotNullSelector = Expressions.WhereNotNullClause<ContactItem>("property");
var contactItems = context.ContactItems.Where(whereNotNullSelector).ToList();
生成的 SQL 不包含 where 子句,因此 where 似乎是在查询解析后执行的。 在正确解决此问题之前,我还需要注意哪些事项?
【问题讨论】:
标签: c# sql-server linq lambda entity-framework-5