private System.Linq.Expressions.Expression<Func<ActionLogs, bool>> GetExpression()
{
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;
}
這是生成我的表達式的函數。爲什麼我的LINQ表達式在LINQ to SQL中不起作用?
,當我做到這一點:
System.Linq.Expressions.Expression<Func<ActionLogs, bool>> expr = GetExpression();
result = blablabla.Where(expr);
這對我說,「W」的參數不在範圍內。
所以問題是,我如何生成我的表達式,這取決於我需要的東西,並將其粘貼到LINQ to SQL查詢中?
它會在哪一行發生錯誤?我需要知道在上面的例子中是從哪裏拋出的錯誤,而不是「行364」 – 2010-09-20 16:51:24
here:result = blablabla.where(expr); – Dmitry 2010-09-20 17:52:32