解决方案是创建一个返回指定属性的表达式并将该表达式传递给Where
:
var query = session.Query<YourType>();
list = query.Where(GetExpression<YourType>(propertyName, filterValue)).ToList();
GetExpression
看起来像这样:
public static Expression<Func<T, bool>> GetExpression<T>(string propertyName,
string filterValue)
{
var parameter = Expression.Parameter(typeof(T));
var property = Expression.Property(parameter, propertyName);
var method = typeof(string).GetMethod("EndsWith", new [] { typeof(string) });
var body = Expression.Call(property, method,
Expression.Constant(filterValue));
return (Expression<Func<T, bool>>)Expression.Lambda(body, parameter);
}