当我执行查询时:
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();
}
怎么了?
要做你想做的事情,你需要做一些类似的事情:
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方法的使用。