Tôi cố gắng để viết một hàm tĩnh để Hoặc hai biểu thức, nhưng nhận được lỗi sau:Expression.Or, Tham số 'mục' không nằm trong phạm vi
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.
phương pháp:
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);
}
chỉnh sửa: thêm thông tin
Biểu thức đang được gửi đến từ phương thức bên dưới, thực thi tốt. nếu có một cách tốt hơn hoặc kết quả tôi là tất cả tai. Ngoài ra, tôi không biết có bao nhiêu người đang được hoặc muốn trước.
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);
}
chỉnh sửa: thêm thông tin thậm chí nhiều hơn
Ngoài ra, là có một cách tốt hơn để làm một hay? Hiện tại .Where (ràng buộc) hoạt động tốt ở nơi có ràng buộc là loại biểu thức>. Làm thế nào tôi có thể làm ở đâu (constraint1 hoặc constraint2) (với ràng buộc n'th)
Cảm ơn bạn trước!
Cảm ơn bạn! Hãy thử và vượt qua cùng một tham số ngay bây giờ – ccook
Bạn của tôi, thật tuyệt vời :) – ccook