使用DbFunctions我得到错误:LINQ to Entities不支持指定的类型成员'Date'

使用DbFunctions我得到错误:LINQ to Entities不支持指定的类型成员'Date'

问题描述:

使用EF 6,即使使用 DbFunctions.TruncateTime

Using EF 6 when I run this code even using DbFunctions.TruncateTime

var touches = analyticRepo.GetAll()
                          .Where(x => DbFunctions.TruncateTime(x.DateCreated.Date) == report.DateCreated.Date);
var test = touches.ToList();

我收到此错误:


LINQ to Entities不支持指定的类型成员'Date'。
只支持初始化器,实体成员和实体导航属性

The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

任何想法如何解决这个问题

Any idea how to solve this.

您可以将日期提升为变量:

You can pull the date up into a variable:

var reportDate = report.DateCreated.Date;
var touches = analyticRepo.GetAll()
                          .Where(x => DbFunctions.TruncateTime(x.DateCreated) == reportDate);
var test = touches.ToList();