2012-04-30 2 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); //この関数は、 –

答えて

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; } 
     } 
関連する問題