다른 것들과 결합하여 필터링을 위해 다소 큰 식 트리에 결합 할 람다 식을 작성하려고합니다. 하위 컬렉션 속성으로 필터링해야 할 때까지 제대로 작동합니다.컬렉션 속성을 필터링하기 위해 동적 식 트리 만들기
루트 객체의 속성 인 컬렉션의 속성에서 Any()를 사용하여 필터링하는 람다 식을 어떻게 작성합니까?
예 :
CurrentDataSource.Offices.Where(o => o.base_Trades.Any(t => t.Name == "test"))
이것은 내가 정적 표현을 구축 할 것입니다하지만 동적으로 구축해야하는 방법이다. 혼란을 드려 죄송합니다.
편집 :
IQueryable<Office> officeQuery = CurrentDataSource.Offices.AsQueryable<Office>();
ParameterExpression pe = Expression.Parameter(typeof(Office), "Office");
ParameterExpression tpe = Expression.Parameter(typeof(Trades), "Trades");
Expression SimpleWhere = null;
Expression ComplexWhere = null;
foreach (ServerSideFilterObject fo in ssfo)
{
SimpleWhere = null;
foreach (String value in fo.FilterValues)
{
if (!CollectionProperties.Contains(fo.PropertyName))
{
//Handle singleton lambda logic here.
Expression left = Expression.Property(pe, typeof(Office).GetProperty(fo.PropertyName));
Expression right = Expression.Constant(value);
if (SimpleWhere == null)
{
SimpleWhere = Expression.Equal(left, right);
}
else
{
Expression e1 = Expression.Equal(left, right);
SimpleWhere = Expression.Or(SimpleWhere, e1);
}
}
else
{
//handle inner Collection lambda logic here.
Expression left = Expression.Property(tpe, typeof(Trades).GetProperty("Name"));
Expression right = Expression.Constant(value);
Expression InnerLambda = Expression.Equal(left, right);
//Problem area.
Expression OfficeAndProperty = Expression.Property(pe, typeof(Office).GetProperty(fo.PropertyName));
Expression OuterLambda = Expression.Call(OfficeAndProperty, typeof(Trades).GetMethod("Any", new Type[] { typeof(Expression) }),InnerLambda);
if (SimpleWhere == null)
SimpleWhere = OuterLambda;
else
SimpleWhere = Expression.Or(SimpleWhere, OuterLambda);
}
}
if (ComplexWhere == null)
ComplexWhere = SimpleWhere;
else
ComplexWhere = Expression.And(ComplexWhere, SimpleWhere);
}
MethodCallExpression whereCallExpression = Expression.Call(typeof(Queryable), "Where", new Type[] { officeQuery.ElementType }, officeQuery.Expression, Expression.Lambda<Func<Office, bool>>(ComplexWhere, new ParameterExpression[] { pe }));
results = officeQuery.Provider.CreateQuery<Office>(whereCallExpression);
표현식 트리를 만드는 방법을 묻는 중입니까? – SLaks
귀하의 예에서 계층 구조가 어떻게 작동하는지 모르겠습니다. 좀 더 자세히 설명해 주시겠습니까? 사무실은 근원지입니까? 그리고 각 사무실에는 거래의 모음이 있습니까? 그리고 당신은 무역의 이름을 걸러 내고 싶습니까 ?? 필터는 내가 잃어버린 부분이다. 죄송합니다. –
아니요, 내부 메서드 호출 및 매개 변수 식을 사용하여 식을 작성하는 데 사용되는 구문을 확신 할 수 없습니다. 이 경우 Any 매개 변수가 정의와 일치하지 않기 때문에 Any()를 찾을 수 없다는 오류가 발생합니다. 이 경우 구문에 어긋나거나 Any()가 사용중인 방식으로 지원되지 않기 때문에 그 이유가 확실하지 않습니다. – George