2012-04-30 1 views
4

내가 그러나Expression <Func <MyClass, bool >> []을 어떻게 결합합니까?

Expression<Func<MyClass,bool>> 

의 배열을 가지고, 내가하고 싶은 모두 함께 해당 유형의 단지 하나의 아이템을 얻을 수 있습니다. 어떻게해야합니까? Expression의 결과를 캐스팅 할 수 있습니까?

+1

샘플로 보여 주시겠습니까? –

+1

나는 이것을 수행하기 위해 과거에 PredicateBuilder를 사용했습니다 - http://www.albahari.com/nutshell/predicatebuilder.aspx –

+0

최종 결과가'Expression >'이되기를 원하십니까? – yamen

답변

4

다음과 같은 확장 방법을 사용하는 경우 : 여기에서

public static Expression<Func<T, bool>> And<T> (this Expression<Func<T, bool>> expr1, 
                 Expression<Func<T, bool>> expr2) 
{ 
    var invokedExpr = Expression.Invoke (expr2, expr1.Parameters.Cast<Expression>()); 
    return Expression.Lambda<Func<T, bool>> 
      (Expression.AndAlso (expr1.Body, invokedExpr), expr1.Parameters); 
} 

을 : http://www.albahari.com/nutshell/predicatebuilder.aspx

그럼 당신은 단지 하나 개의 표현까지 모두 접을이를 작성할 수 있습니다.

public Expression<Func<T, bool>> AggregateAnd(Expression<Func<T,bool>>[] input) 
{ 
    return input.Aggregate((l,r) => l.And(r)); 
} 
+0

위의 코드는 Invoke를 SQL로 변환 할 수 없으므로 대부분의 경우 실제로 작동하지 않습니다. 비슷한하지만 더 호환되는 코드를 찾지 못했습니다. http://stackoverflow.com/questions/110314/linq-to-entities-building-where-clauses-to-test-collections-within-a-many-to-m – Brannon