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 回答