我試圖寫一個靜態函數或兩個表達式,但收到以下錯誤:Expression.Or,參數「項目」不在範圍內
The parameter 'item' is not in scope.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The parameter 'item' is not in scope.
方法:
public static Expression<Func<T, bool>> OrExpressions(Expression<Func<T, bool>> left, Expression<Func<T, bool>> right)
{
// Define the parameter to use
var param = Expression.Parameter(typeof(T), "item");
var filterExpression = Expression.Lambda<Func<T, bool>>
(Expression.Or(
left.Body,
right.Body
), param);
// Build the expression and return it
return (filterExpression);
}
編輯:增加更多的信息
正在或運算表達式是由下面的方法,它執行就好了過來。如果還有更好的辦法或者結果我全都耳熟能詳。另外,我不知道有多少人正在提前。
public static Expression<Func<T, bool>> FilterExpression(string filterBy, object Value, FilterBinaryExpression binaryExpression)
{
// Define the parameter to use
var param = Expression.Parameter(typeof(T), "item");
// Filter expression on the value
switch (binaryExpression)
{
case FilterBinaryExpression.Equal:
{
// Build an expression for "Is the parameter equal to the value" by employing reflection
var filterExpression = Expression.Lambda<Func<T, bool>>
(Expression.Equal(
Expression.Convert(Expression.Property(param, filterBy), typeof(TVal)),
Expression.Constant(Value)
),
param);
// Build the expression and return it
return (filterExpression);
}
編輯:添加更多的信息
或者,有沒有更好的辦法做到的還是?目前.Where(約束)工作得很好,其中約束是類型Expression>。我怎麼辦(約束1或約束2)(約束N'th)
在此先感謝!
謝謝!要去嘗試,現在通過相同的參數 – ccook 2009-01-04 22:25:34
你我的朋友,是驚人的:) – ccook 2009-01-04 22:30:21