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、左側にあるフィールドにもメソッドを適用する必要があります(演算子の左側の上に下線が引かれ、太字になっています)

4

1 に答える 1

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; }
        }
于 2013-04-19T07:50:11.183 に答える