Linq 如何在创建表达式树时同时使用DateTime.Parse()方法(即在表达式左侧和表达式右侧)?

Linq 如何在创建表达式树时同时使用DateTime.Parse()方法(即在表达式左侧和表达式右侧)?,linq,expression-trees,Linq,Expression Trees,因此,我需要在字段上应用DateTime.Parse方法,该字段也位于运算符左侧上方的下划线和粗体上,不确定您要实现什么 1.外卖是干什么用的?必须进行比较的每个属性 2 ExprPrev从未声明过 无论如何,创建该表达式的方法如下所示 lstReport=lstReport.Where(o=>DateTime.Parse(o.Field)==DateTime.Parse(o.FieldValue)); //I am creating above statement dynamically

因此,我需要在字段上应用DateTime.Parse方法,该字段也位于运算符左侧上方的下划线和粗体上,不确定您要实现什么

1.外卖是干什么用的?必须进行比较的每个属性

2 ExprPrev从未声明过

无论如何,创建该表达式的方法如下所示

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();

为什么不做与您在右侧所做的完全相同的事情呢?我尝试过,但不起作用,因为我不知道如何提供参数var methodParam=Expression.Parametertypeofstring,oFC.FieldName;//此函数期望常量值作为第二个参数,而不是fieldname
[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; }
        }