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
方法(在运算符的左侧加下划线和粗体)
答案 0 :(得分: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; }
}