2012-04-30 3 views
1
lstReport=lstReport.Where(o=>DateTime.Parse(o.Field)==DateTime.Parse(o.FieldValue)); 
//I am creating above statement dynamically like this 
var variable = Expression.Variable(typeof(Report)); 
foreach (SQWFilterConstraint oFC in oFilter.LstFilterConstraint) //using this collection I am making dynamic query 
{ 
    Expression ExprLeft =Expression.Property(variable, oFC.Field); 
    MethodInfo methodDateTimeParse = typeof(DateTime).GetMethod("Parse", newType[] { typeof(string) }); 
    var methodParam = Expression.Parameter(typeof(string), oFC.FieldValue); 
    Expression exprRight = Expression.Call(methodDateTimeParse, methodParam); //This is working fine for right side 
} 
var props = new[] { variable }; 
var lambda = Expression.Lambda<Func<Report, bool>>(ExprPrev, props).Compile(); 
ReportList = ReportList.Where(lambda).ToList(); 

그래서 나는 왼쪽에서 (밑줄 연산자의 왼쪽 위의 굵은되는) 제공도 필드에 DateTime.Parse 방법표현식 트리를 만드는 동안 DateTime.Parse() 메서드를 양측 (즉, 표현식 왼쪽과 표현식 오른쪽)으로 사용하는 방법은 무엇입니까?

+0

오른쪽 측면에서 이미 수행 한 것과 똑같은 작업을 수행하지 않는 이유는 무엇입니까? – svick

+0

매개 변수를 제공하는 방법이 없기 때문에 시도했지만 작동하지 않습니다. methodParam = Expression.Parameter (typeof (string), oFC.FieldName); //이 함수는 fieldname이 아닌 두 번째 매개 변수로 상수 값을 필요로합니다. –

답변

0

당신이 달성하려고 무엇 확실하지를 적용해야합니다.

1) foreach는 무엇입니까? 비교해야 할 각 속성?

2) ExprPrev는 절대로 선언되지 않았습니다.

아무튼 그 표현을 만드는 방법은 다음과 같습니다.

[TestMethod] 
     public void TestDateTimeParse() 
     { 
      var variable = Expression.Variable(typeof (Report)); 

      var parseMethodInfo = typeof (DateTime).GetMethod("Parse", new[] {typeof (string)}); 
      var left = Expression.Call(parseMethodInfo, Expression.Property(variable, "Field")); 
      var right = Expression.Call(parseMethodInfo, Expression.Property(variable, "FieldValue")); 
      var equals = Expression.Equal(left, right); 

      var expression = Expression.Lambda<Func<Report, bool>>(equals, variable).Compile(); 

      var target = new Report {Field = DateTime.Now.ToString()}; 
      target.FieldValue = target.Field; 
      expression(target).Should().Be.True(); 
     } 

     public class Report 
     { 
      public string Field { get; set; } 
      public string FieldValue { get; set; } 
     } 
관련 문제