2016-01-11 30 views
6

मैं इस तरह एक विधि है:प्रतिनिधि प्रकार में लैम्ब्डा अभिव्यक्ति कनवर्ट नहीं कर सकता

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

मैं की तरह

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

एक और कक्षा में एक विधि कॉल करना है लेकिन मैं हमेशा इस त्रुटि मिलती है:

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

इस काम को पाने के लिए मुझे क्या बदलना है?

संपादित करें:

मैं इकाई की रूपरेखा 6 का उपयोग करें और अगर मैं) कोई भी (का उपयोग कहाँ() के बजाय, मैं हमेशा ही 1 परिणाम वापस पाने ... मैं अपने एफई-कार्यान्वयन के लिए अभिव्यक्ति पास करना चाहते हैं:

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

आप शायद मतलब 'कोई भी()' के बजाय 'कहाँ()'। आपका 'Func ' को 'बूल' वापस करने की आवश्यकता है, लेकिन 'कहां' वापस आ रहा है 'IENumerable '। – haim770

+0

वे संगत नहीं हैं। –

+1

क्या आप वाकई 'GetEntitiesWithPredicate (अभिव्यक्ति > predicate) का मतलब है' और न केवल 'GetEntitiesWithPredicate (Func predicate)'? आपको 'अभिव्यक्ति' की आवश्यकता क्यों है? –

उत्तर

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; } 
} 
संबंधित मुद्दे