【发布时间】:2016-04-15 18:40:49
【问题描述】:
我有这样的方法:
public ICollection<T> GetEntitiesWithPredicate(Expression<Func<T, bool>> predicate)
{
// ...
}
我在另一个类中调用方法
service.GetEntitiesWithPredicate(x => x.FoobarCollection.Where(y => y.Text.Contains(SearchText)));
但我总是收到此错误:
Lambda expression cannot be converted to '<typename>' because '<typename>' is not a delegate type
我必须改变什么才能完成这项工作?
编辑:
我使用 Entity Framework 6,如果我使用 Any() 而不是 Where(),我总是只能得到 1 个结果...我想将表达式传递给我的 EF 实现:
public ICollection<T> GetEntriesWithPredicate(Expression<Func<T, bool>> predicate)
{
using (var ctx = new DataContext())
{
return query.Where(predicate).ToList();
}
}
【问题讨论】:
-
您可能指的是
Any(),而不是Where()。您的Func<T, bool>需要返回bool,但Where正在返回IEnumerable<T>。 -
那些不兼容。
-
你确定你的意思是
GetEntitiesWithPredicate(Expression<Func<T, bool>> predicate)而不仅仅是GetEntitiesWithPredicate(Func<T, bool>predicate)吗?为什么需要Expression? -
@PeterA.Schneider,因为在方法实现中,谓词被传递给一些 Linq 提供者(如实体框架)
-
@haim770 你能检查我的编辑吗