표현 트리의 일부 속성 값을 읽고 싶습니다. 일부 계산을 진행할 수 있습니다.표현 트리에서 속성의 값을 읽는 방법은 무엇입니까?
var products = db.Products
.Where(GetPredicate())
.ToList();
private Expression<Func<Product, bool>> GetPredicate()
{
ParameterExpression pe = Expression.Parameter(typeof(Product), "p");
Expression exp0 = Expression.Property(pe, "Price");
//I'd like to know the value of the 'Price'
// so I can do some calculation, then check whether
//this particular product meet the criteria...
Expression body = Expression.Constant(Result); //result is a boolean
var expr = Expression.Lambda<Func<Product, bool>>(body, new ParameterExpression[] { pe });
return expr;
}
Expression.Convert를 사용하려고 했습니까? – napi15
[표현식 트리를 사용하여 개체 속성 읽기] (https://stackoverflow.com/questions/16436323/reading-properties-of-an-object-with-expression-trees) – johnnyRose
식 트리를 만들 때 'GetPredicate'에서는'Product' 객체가 없으므로'Price' 값을 갖지 않습니다. 따라서 컴파일 된 표현식 트리에 전달하기 전에 실제 가치를 얻을 수는 없습니다. 그러나 표현 트리에 원하는대로 비교와 제약 조건을 추가 할 수 있습니다. 뭔가 다음과 같이 보입니다 :'Expression.Add (exp0, Expression.Constant (156));'그런 다음 반환 된 값 등을 확인할 수 있습니다. 그러나 값을 아직 알지 못한다면 입력 값이 비교된다는 것을 알 것입니다 내가 뭔가를 놓치거나 당신의 질문을 이해하지 못했습니까? –