मैं कुछ समान उपयोग करता हूं, जहां मैं एक क्वेरी में फ़िल्टर जोड़ता हूं।
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 उदाहरण होने से, आप और अधिक आसानी से समझ सकता है कि कैसे हम नाम से विभिन्न विभिन्न तरीकों कॉल कर सकते हैं।
पहले कुछ उत्तरों को छोड़कर प्रारंभ करें, जैसे कि यह एक 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
यहां एक और डुप्ली: http://stackoverflow.com/questions/1270783/how-to-combine-two-expressions-result-exp1exp2 – Kamarey
थैक्स, –