Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将谓词作为参数传递_C#_Sqlite_Predicate - Fatal编程技术网

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)