I'm using C# 2010 .NET 4.0 and I have a List<T> collection returns that i needing to build a dynamic LINQ query on.
I'm utilizing the Predicate Builder referenced here
This works fine if I have 1 filter criteria, but if i have 2 or more, then, when the query.compile() is called, i get this error:
variable 'tmp' of type 'Check21Tools.IncomingReturn' referenced from scope '', but it is not defined
Code:
Code:Code:Expression<Func<Check21Tools.IncomingReturn, bool>> query = null; bool hasFilterItems = false; if (filterArray != null) { foreach (string s in filterArray) { if (s == string.Empty) { break; } else { hasFilterItems = true; Int64 id = Int64.Parse(s); query = query.Or(tmp => tmp.ID == id); } } } if (hasFilterItems) { returns = returns.Where(query.Compile()).CreateFromEnumerable<Check21Tools.IncomingReturns, Check21Tools.IncomingReturn>(); }
Code:public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) { if (expr1 == null) return expr2; return Expression.Lambda<Func<T, bool>> (Expression.OrElse(expr1.Body, expr2.Body), expr1.Parameters); }




Reply With Quote
