using Expressions [modified]
-
public static class PredicateBuilder
{
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }public static Expression<Func<T, bool>> Or<T>(this <Func<T, bool>> expr1, <Func<T, bool>> expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>()); return Expression.Lambda<Func<T, bool>> (Expression.Or(expr1.Body, invokedExpr), expr1.Parameters); } public static Expression<Func<T, bool>> And<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.And(expr1.Body, invokedExpr), expr1.Parameters); } } public static Expression<Func<MyObj, bool>> AndKeyPredicate(MyObj o) { var predicate = PredicateBuilder.False<myobj>(); foreach (string s in o.GetAndKeyItems()) // I need MyObj in here instead of "MyObj o" parameter { predicate = predicate.Or(p => MyText.All.Contains(s)); } return predicate; }
IQueryable<myobj> _ob = xxx.AsQueryable();
var query = _ob.Where(AndKeyPredicate(xxx[0]));
I must take the each MyObj item for using in AndKeyPredicate parameter. we have 2 options: 1) var query = from y in _ob where AndKeyPredicate(y) select y; it doesn't work 2) we can get instance of MyObj from public static Expression<Func<T, bool>> AndKeyPredicate() predicate know p is a MyObj: predicate = predicate.Or(p => MyText.All.Contains(s)); but I have to use MyObj before //foreach (string s in o.GetAndKeyItems()) predicate = predicate.Or(p => MyText.All.Contains(s)); HOW?????? please help me
modified on Friday, July 18, 2008 8:14 AM