Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架LINQ查询问题wtih表达式树_C#_Linq_Expression Trees - Fatal编程技术网

C# 实体框架LINQ查询问题wtih表达式树

C# 实体框架LINQ查询问题wtih表达式树,c#,linq,expression-trees,C#,Linq,Expression Trees,我编写了一个简单的LINQ查询,只使用EF6DbFunction比较日期部分,它运行良好,我从数据库中得到结果 var dateDifference = DateTime.Now.Date; var queryableData = DataContext.Single .Where(s => dateDifference == DbFunctions.CreateDateTime(s.Created.Year, s.Created.Month, s.Created.Da

我编写了一个简单的LINQ查询,只使用EF6DbFunction比较日期部分,它运行良好,我从数据库中得到结果

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

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

当你可以编写一个lambda并让编译器为你做这件事的时候,你为什么要从头开始编写呢?我有一个动态搜索条件,简单的lambda只是用来测试它是否有效,实际上我想用表达式树生成动态linq查询,这是现在唯一剩下的东西。想知道我是否发现自己做错了什么。看到有人投票结束这个问题,应该有点惊讶。我向那些不知道答案的人提出的请求至少不要做出愚蠢的请求。如果我错了,请纠正我,但在第一个示例中,您将根据CreateDateTimes检查dateDifference。在第二个示例中,您将根据参数p检查CreateDateTimeDifference。你的常量表达式不应该是dateDifference吗?你的参数表达式应该是围绕s.Created构建的吗?是的,你做对了。我不知道用表达式树怎么可能?我确实在不断地做错事。。如何将linq查询转换为适当的表达式树?