private System.Linq.Expressions.Expression<Func<ActionLogs, bool>> G开发者_StackOverflow社区etExpression()
{
Expression<Func<ActionLogs, bool>> expr = w => w.ID != -1;
if (ActionDate != null)
{
Expression<Func<ActionLogs, bool>> byDate = w => w.DateAction == ActionDate;
var body = Expression.AndAlso(expr.Body, byDate.Body);
expr = Expression.Lambda<Func<ActionLogs, bool>>(body, expr.Parameters[0]);
}
if (ActionType != ActionTypeEnum.Empty)
{
Expression<Func<ActionLogs, bool>> byActionType = w => w.ActionTypeID == (int)ActionType;
var body = Expression.AndAlso(expr.Body, byActionType.Body);
expr = Expression.Lambda<Func<ActionLogs, bool>>(body, expr.Parameters[0]);
}
if (!String.IsNullOrWhiteSpace(AuthorLogin))
{
Expression<Func<ActionLogs, bool>> byLogin = w => w.User.LoginName == AuthorLogin;
var body = Expression.AndAlso(expr.Body, byLogin.Body);
expr = Expression.Lambda<Func<ActionLogs, bool>>(body, expr.Parameters[0]);
}
if (!String.IsNullOrWhiteSpace(AdditionalInfo))
{
Expression<Func<ActionLogs, bool>> byAdditionalInfo = w => w.DescriptionText.Contains(AdditionalInfo);
var body = Expression.AndAlso(expr.Body, byAdditionalInfo.Body);
expr = Expression.Lambda<Func<ActionLogs, bool>>(body, expr.Parameters[0]);
}
return expr;
}
This is the function that generate my expression.
and when i do this:
System.Linq.Expressions.Expression<Func<ActionLogs, bool>> expr = GetExpression();
result = blablabla.Where(expr);
It says to me that The parameter 'w' is not in scope.
So the question is, how i can generate my expression, that depends on something i need, and paste it into LINQ to SQL query?
public Expression<Func<T, bool>> Combine<T>(Expression<Func<T, Boolean>> first, Expression<Func<T, Boolean>> second)
{
var toInvoke = Expression.Invoke(second, first.Parameters.Cast<Expression>());
return (Expression.Lambda<Func<T, Boolean>>(Expression.AndAlso(first.Body, toInvoke), first.Parameters));
}
This function was very helpful. It combined two expressions and built correct tree.
Could it be that you are trying to return an Expression.Lambda<T>
and then assign it to an object that is a SON of it on its hierarchy(System.Linq.Expressions.Expression<T>
)
Please take a look at http://msdn.microsoft.com/en-us/library/system.linq.expressions.lambdaexpression.aspx
Hope that helps,
精彩评论