Gig*_*igi 2 c# linq linq-to-entities entity-framework
当我执行我的查询时:
rs.Select(x => x.id).ToArray();
Run Code Online (Sandbox Code Playgroud)
我得到了这个错误:
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;
}
Run Code Online (Sandbox Code Playgroud)
我在这种模式下调用方法:
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();
}
Run Code Online (Sandbox Code Playgroud)
怎么了?
要做你想做的事,你需要做以下事情:
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;
}
Run Code Online (Sandbox Code Playgroud)
你可以看到你需要做一些表达树管道.您可以像以前一样调用该方法.
现在......是否真的可以按照@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;
}
Run Code Online (Sandbox Code Playgroud)
注意使用Invoke()和Expand()LinqKit方法.
| 归档时间: |
|
| 查看次数: |
7533 次 |
| 最近记录: |