표현식 트리의 수정에 어려움을 겪고 있습니다. 예제를 단순화하여 여기에 표시하기 쉽도록했습니다.표현식 트리에서 유형 바꾸기
public class Filter
{
public string FilterableProperty1 { get; set; }
public string FilterableProperty2 { get; set; }
}
public class Entity
{
public string FilterableProperty1 { get; set; }
public string FilterableProperty2 { get; set; }
public string NonFilterableProperty { get; set; }
}
필터 클래스의 모든 속성도 Entity 클래스에 있습니다. 지금은 이런 방법으로 원하는 개체를 반환하는 필터 클래스를 사용하고 싶습니다 :
public IEnumerable<Entity> GetEntities(Expression<Func<Filter, bool>> filter)
{
Expression<Func<Entity, bool>> convertedFilter = Expression.Lambda<Func<Entity, bool>>(
filter.Body,
Expression.Parameter(typeof(Entity), filter.Parameters[0].Name));
using (MyEntities entities = new MyEntities())
{
return entities.Entities.Where(convertedFilter);
}
}
을 그래서 기본적으로 난 그냥 expression 매개 변수의 유형을 변경합니다. 이제 다음과 같은 함수를 호출하면 :
public IEnumerable<Entity> GetFilteredEntities()
{
return GetEntities(x => x.FilterableProperty1 == "Test");
}
예외가 발생합니다. 매개 변수 x가 지정된 쿼리 식에서 발견되지 않았습니다. 분명히 ParameterExpression을 대체하여 무언가를 깨고 있습니다. 원래 표현식의 본문을 대신하는 (또는 다시 작성하는) 올바른 유형의 새 표현식을 만들려면 어떻게해야합니까?
톰 감사합니다. 내 하루를 구하십시오! –