我有一个类似的业务层调用:
CustomerRepository.Get(c => SqlFunctions.PatIndex("%" + arg + "%", c.FirstName));
我正在尝试使用表达式来构建它:
public virtual IEnumerable<TEntity> Like(string LikeString, string Target)
{
MethodInfo method = typeof(SqlFunctions).GetMethod("PatIndex");
var arg1 = Expression.Constant(LikeString);
var item = Expression.Parameter(typeof(TEntity), "item");
var prop = Expression.Property(item, Target);
MethodCallExpression resultExp =
Expression.Call(method, arg1, prop);
var value = Expression.Constant(0, typeof(int?));
var greaterThan = Expression.GreaterThan(resultExp, value);
var lambda = Expression.Lambda<Func<TEntity, bool>>(greaterThan, item);
var result = Repo<TEntity>.Get().AsQueryable().Where(lambda);
return result;
}
当我调用上面的方法时,我得到以下异常:
This function can only be invoked from LINQ to Entities.
有关如何通过这个或做我想做的事的任何想法?我的表达式代码看起来不错吗?
答案 0 :(得分:1)
找出问题
来自:
var result = Repo<TEntity>.Get().AsQueryable().Where(lambda);
为:
var result = Repo<TEntity>.Get(lambda);