当我执行查询时:
rs.Select(x => x.id).ToArray();
我收到此错误:
LINQ to Entities中不支持LINQ表达式节点类型“Invoke”
这是生成错误的方法(可能 func(x)):
public IQueryable<TEntity> Compare<TEntity>(IQueryable<TEntity> source, Func<TEntity, int> func)
{
IQueryable<TEntity> res = source;
if (!this.LBoundIsNull) res = res.Where(x => func(x) >= _lBound);
if (!this.UBoundIsNull) res = res.Where(x => func(x) <= _uBound);
return res;
}
我在这种模式下调用方法:
Document doc = new Document();
doc.Number = new RangeValues(lBound, null);
using (MyEntities db = new MyEntities())
{
var rs = db.documents;
if (doc.Number != null) rs = doc.Numero.Compare(rs, x => x.number);
long[] id = rs.Select(x => x.id).ToArray();
}
有什么问题?
答案 0 :(得分:5)
要做你想做的事,你需要做类似的事情:
public static IQueryable<TEntity> Compare<TEntity>(IQueryable<TEntity> source, Expression<Func<TEntity, int>> func)
{
IQueryable<TEntity> res = source;
if (!LBoundIsNull)
{
Expression ge = Expression.GreaterThanOrEqual(func.Body, Expression.Constant(_lBound));
var lambda = Expression.Lambda<Func<TEntity, bool>>(ge, func.Parameters);
res = res.Where(lambda);
}
if (!UBoundIsNull)
{
Expression le = Expression.LessThanOrEqual(func.Body, Expression.Constant(_uBound));
var lambda = Expression.Lambda<Func<TEntity, bool>>(le, func.Parameters);
res = res.Where(lambda);
}
return res;
}
你可以看到你需要做一些表达树管道。您可以像以前一样调用该方法。
现在......是否真的可以按照@jbl的建议使用LinqKit?是的......通过摇动一点魔杖......
using LinqKit;
public static IQueryable<TEntity> Compare<TEntity>(IQueryable<TEntity> source, Expression<Func<TEntity, int>> func)
{
IQueryable<TEntity> res = source;
if (!LBoundIsNull)
{
Expression<Func<TEntity, bool>> lambda = x => func.Invoke(x) >= _lBound;
res = res.Where(lambda.Expand());
}
if (!UBoundIsNull)
{
Expression<Func<TEntity, bool>> lambda = x => func.Invoke(x) <= _uBound;
res = res.Where(lambda.Expand());
}
return res;
}
请注意使用Invoke()
和Expand()
LinqKit方法。