2010-03-17 25 views
7

किसी को भी यह कैसेLINQ अभिव्यक्ति <समारोह <T, bool>> .Contains()

public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, 
     Expression<Func<T, bool>> expr2) 
{ 
    var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>()); 
    return Expression.Lambda<Func<T, bool>> 
       (Expression.OrElse(expr1.Body, invokedExpr), expr1.Parameters); 
} 

पूरा करने के लिए एक .Contains (स्ट्रिंग) Linq भाव का उपयोग कर समारोह बनाने के लिए, या यहाँ तक कि बनाने के लिए एक विधेय की एक विचार मिल गया है की equavalent इसके लिए कुछ अनुकरणीय आदर्श होगा?

+2

पहले कुछ उत्तरों को छोड़कर प्रारंभ करें, जैसे कि यह एक http://stackoverflow.com/questions/1648270/how-to-determine-what-happens-behind-the-scene-in-net/1648306#1648306 और यह http://stackoverflow.com/questions/2331927/linq-to-xml-replace-child-nodes-but-keep-state/2332087#2332087। – Steven

+0

यहां एक और डुप्ली: http://stackoverflow.com/questions/1270783/how-to-combine-two-expressions-result-exp1exp2 – Kamarey

+0

थैक्स, –

उत्तर

6
public static Expression<Func<string, bool>> StringContains(string subString) 
{ 
    MethodInfo contains = typeof(string).GetMethod("Contains"); 
    ParameterExpression param = Expression.Parameter(typeof(string), "s"); 
    return Expression.Call(param, contains, Expression.Constant(subString, typeof(string))); 
} 

... 

// s => s.Contains("hello") 
Expression<Func<string, bool>> predicate = StringContains("hello"); 
+0

चाल क्या थी, thx –

+0

बस कुछ स्पष्टता देने के लिए, उपरोक्त वर्णित यह समाधान काम नहीं करेगा यदि आप इसे सीधे उपयोग करते हैं, तो मैंने केवल कुछ सामग्री जैसे MethodInfo और पैरामीटर एक्स्प्रेशन का उपयोग किया है। –

1

मैं कुछ समान उपयोग करता हूं, जहां मैं एक क्वेरी में फ़िल्टर जोड़ता हूं।

public static Expression<Func<TypeOfParent, bool>> PropertyStartsWith<TypeOfParent, TypeOfPropery>(PropertyInfo property, TypeOfPropery value) 
{ 
    var parent = Expression.Parameter(typeof(TypeOfParent)); 
    MethodInfo method = typeof(string).GetMethod("StartsWith",new Type[] { typeof(TypeOfPropery) }); 
    var expressionBody = Expression.Call(Expression.Property(parent, property), method, Expression.Constant(value)); 
    return Expression.Lambda<Func<TypeOfParent, bool>>(expressionBody, parent); 
} 

उपयोग, उस संपत्ति के खिलाफ फ़िल्टर लागू करने के लिए जिसका नाम कुंजी से मेल खाता है, और आपूर्ति मूल्य का उपयोग करके, मूल्य।

public static IQueryable<T> ApplyParameters<T>(this IQueryable<T> query, List<GridParameter> gridParameters) 
{ 

    // Foreach Parameter in List 
    // If Filter Operation is StartsWith 
    var propertyInfo = typeof(T).GetProperty(parameter.Key); 
    query = query.Where(PropertyStartsWith<T, string>(propertyInfo, parameter.Value)); 
} 

और हाँ, इस विधि से काम करता है शामिल हैं के साथ:

 public static Expression<Func<TypeOfParent, bool>> PropertyContains<TypeOfParent, TypeOfPropery>(PropertyInfo property, TypeOfPropery value) 
    { 
     var parent = Expression.Parameter(typeof(TypeOfParent)); 
     MethodInfo method = typeof(string).GetMethod("Contains", new Type[] { typeof(TypeOfPropery) }); 
     var expressionBody = Expression.Call(Expression.Property(parent, property), method, Expression.Constant(value)); 
     return Expression.Lambda<Func<TypeOfParent, bool>>(expressionBody, parent); 
    } 

उन 2 उदाहरण होने से, आप और अधिक आसानी से समझ सकता है कि कैसे हम नाम से विभिन्न विभिन्न तरीकों कॉल कर सकते हैं।

संबंधित मुद्दे