【发布时间】:2015-04-09 13:34:31
【问题描述】:
当我执行查询时:
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();
}
怎么了?
【问题讨论】:
-
是
func(x)被翻译成Sql 用户定义的函数调用? -
您收到的错误消息告诉您确切地出了什么问题。 EF 不支持调用函数。
-
jbl,
func(x)用于检索属性的值(本例为x.number)。 -
@Gigi 你不能那样做......你需要做一些表达式树管道来做到这一点。
-
LinqKit 在这里应该有所帮助:github.com/scottksmith95/LINQKit
标签: c# linq entity-framework linq-to-entities