2017-02-03 67 views
0

我想通过传递谓词作为参数来获取一定数量的元素。不过,我收到以下错误:将谓词作为参数传递

Cannot implicitly convert type System.Collections.Generic.IEnumerable < Students.Entities> to bool

var names = await Task.Run(() => Repository.GetWithNChildren(e => e.Entities.Take(offset))); 


public List<T> GetWithNChildren(Expression<Func<T, bool>> predicate = null) 
{ 
    var db = _factory.GetConnectionWithLock(); 
    using (db.Lock()) 
    { 
     return db.GetAllWithChildren(predicate, true); 
    } 
} 

GetAllWithChildren是在SQLite的类

namespace SQLiteNetExtensions.Extensions 
{ 
    public static class ReadOperations 
    { 
     public static bool EnableRuntimeAssertions; 

     public static T FindWithChildren<T>(this SQLiteConnection conn, object pk, bool recursive = false) where T : class; 
     public static List<T> GetAllWithChildren<T>(this SQLiteConnection conn, Expression<Func<T, bool>> filter = null, bool recursive = false) where T : class; 
     public static void GetChild<T>(this SQLiteConnection conn, T element, string relationshipProperty, bool recursive = false); 
     public static void GetChild<T>(this SQLiteConnection conn, T element, Expression<Func<T, object>> propertyExpression, bool recursive = false); 
     public static void GetChild<T>(this SQLiteConnection conn, T element, PropertyInfo relationshipProperty, bool recursive = false); 
     public static void GetChildren<T>(this SQLiteConnection conn, T element, bool recursive = false); 
     public static T GetWithChildren<T>(this SQLiteConnection conn, object pk, bool recursive = false) where T : class; 
    } 
} 
+0

哪一行有错误 –

+0

第一行有错误 – hotspring

+0

显然,'的IEnumerable '的'。取(返回)'不能被转换为“布尔”。什么是如此令人惊讶? –

回答

1

Func<T, bool>的方法意味着一个函数,它在T并返回bool

函数Take(int n)需要IEnumerable<T>并返回一个IEnumerable<T>,最多有n成员。

您需要将e.Entities.Take(offset)更改为返回bool或开关Expression<Func<T, bool>> predicate = nullExpression<Func<T, U>> predicate = null并将GetAllWithChildren更改为采用相同类型。

+0

一旦我将其更改为'',然后'GetAllWithChilldren'抱怨,因为它只接受'',请参阅我的更新问题 – hotspring

+0

我所需要的只是不获取所有项目,仅获取某些项目(偏移量)。 – hotspring

1

错误是正确的。
这个表达式:

Expression<Func<T, bool>> 

的意思是 “我把T的实例,并会返回一个布尔”

但你回报booelean:

e => e.Entities.Take(offset) 

这是因为 Take(..)不返回布尔值,而是一个IEnumerable的实体。

要修复它 - 你可以试试:

e => e.Entities.Take(offset).Count() > 3 
-2

我想你忘了你的申报方法一般。所以,不是

... GetWithNChildren(Expression<Func<T, bool>> predicate = null) 

你应该有:

GetWithNChildren<T>(Expression<Func<T, bool>> predicate = null) 
+0

如果是这样的话,他会得到一个不同的错误,T很可能是在课堂上定义的。 –

+0

谢谢Scott,这可能是对的。 ;-) –