2016-01-11 20 views
6

mam metody takie jak to:Nie można przekonwertować wyrażenia lambda na typ delegata

public ICollection<T> GetEntitiesWithPredicate(Expression<Func<T, bool>> predicate) 
{ 
      // ... 
} 

robię wywołanie metody w innej klasy jak

service.GetEntitiesWithPredicate(x => x.FoobarCollection.Where(y => y.Text.Contains(SearchText))); 

ale zawsze otrzymuję ten błąd:

Lambda expression cannot be converted to '<typename>' because '<typename>' is not a delegate type 

Co muszę zmienić, aby uzyskać tę pracę?

Edit:

używam Entity Framework 6 i jeśli mogę użyć Obojętnie() zamiast Gdzie(), zawsze dostać tylko 1 wynik z powrotem ... Chcę przekazać wyraz mojego EF-realizacji:

public ICollection<T> GetEntriesWithPredicate(Expression<Func<T, bool>> predicate) 
    { 
     using (var ctx = new DataContext()) 
     { 
      return query.Where(predicate).ToList(); 
     } 
    } 
+11

prawdopodobnie oznaczało 'Obojętnie()' 'Gdzie zamiast()'. Twój 'Func ' musi zwrócić 'bool', ale' Where' zwraca 'IEnumerable '. – haim770

+0

te nie są kompatybilne. –

+1

Czy na pewno masz na myśli 'GetEntitiesWithPredicate (wyrażenie > predykat)', a nie tylko 'GetEntitiesWithPredicate (Func predicate)'? Dlaczego potrzebujesz "Wyrażenie"? –

Odpowiedz

0
class Program 
{ 
    static void Main(string[] args) 
    { 
     var o = new Foo { }; 

     var f = o.GetEntitiesWithPredicate(a => a.MyProperty.Where(b => b.MyProperty > 0).ToList().Count == 2); // f.MyProperty == 9 true 
    } 
} 

class Foo 
{ 
    public ICollection<T> GetEntitiesWithPredicate(Expression<Func<T, bool>> predicate) 
    { 
     var t = predicate.Compile(); 

     var d = t.Invoke(new T { MyProperty = new List<Y> { new Y { MyProperty = 10 }, new Y { MyProperty = 10 } } }); 



     if (d) return new List<T> { new T { MyProperty = new List<Y> { new Y { MyProperty = 9 } } } }; 

     return null; 
    } 
} 

class T 
{ 
    public T() { } 
    public List<Y> MyProperty { get; set; } 
} 

class Y 
{ 
    public int MyProperty { get; set; } 
} 
Powiązane problemy