Cannot convert lambda expression to delegate type

I have a way like this:

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

I am calling a method call in another class, for example

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

but I always get this error:

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

What do I need to change to get this job?

Edit:

I use Entity Framework 6, and if I use Any () instead of Where (), I always get only one result back ... I want to pass the expression to my EF implementation:

  public ICollection<T> GetEntriesWithPredicate(Expression<Func<T, bool>> predicate) { using (var ctx = new DataContext()) { return query.Where(predicate).ToList(); } } 
+6
source share
1 answer
 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; } } 
0
source

All Articles