实体框架LINQ查询问题与表达式树

时间:2014-11-18 18:43:03

标签: c# linq expression-trees

我通过仅使用EF6 Dbfunction比较日期部分编写了一个简单的LINQ查询,它工作正常,我从db获得结果

var dateDifference = DateTime.Now.Date;
var queryableData = DataContext.Single
       .Where(s => dateDifference  ==
 DbFunctions.CreateDateTime(s.Created.Year, s.Created.Month,
 s.Created.Day, 0, 0, 0)).ToList();

但是,如果我尝试使用表达式树编写相同的LINQ查询,我没有得到任何结果,因为查询执行正常而没有任何错误

var  dateDifference = DateTime.Now.Date;
var parameter = Expression.Parameter(typeof(TSource), "p");
propertyReference = Expression.PropertyOrField(parameter, "Created");

var constantReference = Expression.Call(typeof(DbFunctions), "CreateDateTime",
          null,
          new Expression[]
          {                                      
              Expression.Convert(Expression.Constant(dateDifference.Year),typeof(int?)),
              Expression.Convert(Expression.Constant(dateDifference.Month),typeof(int?)),
              Expression.Convert(Expression.Constant(dateDifference.Day),typeof(int?)),
              Expression.Convert(Expression.Constant(0),typeof(int?)),
              Expression.Convert(Expression.Constant(0),typeof(int?)),
              Expression.Convert(Expression.Constant(0),typeof(double?)),
          });

Expression expression = Expression.Equal(propertyReference,constantReference);

但是上面生成的表达似乎并没有产生任何结果。有什么建议吗?

0 个答案:

没有答案