2016-01-11 21 views
6

Ho un metodo come questo:Impossibile convertire espressioni lambda in tipo delegato

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

faccio una chiamata di metodo in un'altra classe come

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

ma ottengo sempre questo errore:

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

Cosa devo cambiare per ottenere questo lavoro?

Edit:

Io uso Entity Framework 6 e se uso Qualsiasi() invece di Dove(), ho sempre arrivare solo 1 risultato indietro ... voglio passare l'espressione al mio EF-implementazione:

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

probabilmente dire 'Qualsiasi()' invece di 'Dove()'. Il tuo 'Func ' deve restituire 'bool' ma' Where' sta restituendo 'IEnumerable '. – haim770

+0

quelli non sono compatibili. –

+1

Sei sicuro di voler dire 'GetEntitiesWithPredicate (Espressione > predicato)' e non solo 'GetEntitiesWithPredicate (Func predicato)'? Perché hai bisogno dell'espressione? –

risposta

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; } 
} 
Problemi correlati