C# 将谓词作为参数传递
我试图通过将谓词作为参数传递来获得一定数量的元素。但我得到了以下错误: 无法将类型C# 将谓词作为参数传递,c#,sqlite,predicate,C#,Sqlite,Predicate,我试图通过将谓词作为参数传递来获得一定数量的元素。但我得到了以下错误: 无法将类型System.Collections.Generic.IEnumerable隐式转换为bool 值Func表示接受T并返回bool的函数 函数Take(int n)接收IEnumerable并返回一个最多包含n成员的IEnumerable 您需要将e.Entities.Take(offset)更改为返回布尔值或将Expression predicate=null切换为Expression predicate=nul
System.Collections.Generic.IEnumerable
隐式转换为bool
值Func
表示接受T
并返回bool
的函数
函数Take(int n)
接收IEnumerable
并返回一个最多包含n
成员的IEnumerable
您需要将e.Entities.Take(offset)
更改为返回布尔值或将Expression predicate=null
切换为Expression predicate=null
并将GetAllWithChildren
更改为相同类型。错误正确。这句话:
Expression<Func<T, bool>>
这是因为
Take(…)
不返回布尔值,而是实体的IEnumerable
要解决此问题,请尝试以下操作:
e => e.Entities.Take(offset).Count() > 3
我认为您忘记了将方法声明为泛型。因此,与其
... GetWithNChildren(Expression<Func<T, bool>> predicate = null)
。。。GetWithNChildren(表达式谓词=null)
你应该:
GetWithNChildren<T>(Expression<Func<T, bool>> predicate = null)
getWithChildren(表达式谓词=null)
哪一行有错误第一行有错误显然,.Take()
返回的IEnumerable
无法转换为bool
。什么事这么令人惊讶?问题是什么?传递返回IEnumerable的e.Entities.Take(offset),谓词必须是返回IEnumerable的表达式bool@NicoSchertler如何解决?如果是这样的话,他会得到一个不同的错误,T很可能是在类级别定义的。谢谢Scott,这可能是真的一旦我将其更改为
,然后GetAllWithChildren
投诉,因为它只接受
,请参阅我的更新问题。我所需要的不是获取所有项目,而是仅获取某些项目(偏移量)。
e => e.Entities.Take(offset).Count() > 3
... GetWithNChildren(Expression<Func<T, bool>> predicate = null)
GetWithNChildren<T>(Expression<Func<T, bool>> predicate = null)