C#Linq仅比较datetime属性中的日期
我拥有以下Linq to实体:C#Linq仅比较datetime属性中的日期,c#,linq,linq-to-entities,C#,Linq,Linq To Entities,我拥有以下Linq to实体: var details = Uow.EmployeeAttendances.GetAllReadOnly() .Where(a => a.EmployeeId == summary.EmployeeId && a.Timestamp == summary.Date) .ToList(); summary.Date只是日期部分,因此值如下:“2014-07-20 00:00:00” 问题在于a.TimeStamp是一个日期时间
var details = Uow.EmployeeAttendances.GetAllReadOnly()
.Where(a => a.EmployeeId == summary.EmployeeId && a.Timestamp == summary.Date)
.ToList();
summary.Date
只是日期部分,因此值如下:“2014-07-20 00:00:00”
问题在于a.TimeStamp
是一个日期时间字段,包含日期和时间
所以上面的查询总是返回空的
有没有办法把时间戳转换成日期,这样我就可以比较苹果和苹果了
我得到的错误是:
The specified type member 'Date' is not supported in LINQ to Entities
非常感谢。
DateTime
字段有一个
var details =
Uow
.EmployeeAttendances
.GetAllReadOnly()
.Where(a => a.EmployeeId == summary.EmployeeId
&& a.TimeStamp.Date == summary.Date)
.ToList();
DateTime
字段有一个
var details =
Uow
.EmployeeAttendances
.GetAllReadOnly()
.Where(a => a.EmployeeId == summary.EmployeeId
&& a.TimeStamp.Date == summary.Date)
.ToList();
正确的解决办法是:
在实体框架6中,必须使用DbFunctions.TruncateTime
var dates = Uow.EmployeeAttendances.GetAllReadOnly()
.Where(a => a.EmployeeId == summary.EmployeeId && System.Data.Entity.DbFunctions.TruncateTime(a.Timestamp) == attendanceDate)
.OrderBy(a=> a.Timestamp)
.ToList();
正确的解决办法是:
在实体框架6中,必须使用DbFunctions.TruncateTime
var dates = Uow.EmployeeAttendances.GetAllReadOnly()
.Where(a => a.EmployeeId == summary.EmployeeId && System.Data.Entity.DbFunctions.TruncateTime(a.Timestamp) == attendanceDate)
.OrderBy(a=> a.Timestamp)
.ToList();
作为使用DbFunctions.TruncateTime的替代方法,您只需执行以下操作:
var from = summary.Date;
var to = summary.Date.AddDays(1);
var details = Uow.EmployeeAttendances
.GetAllReadOnly()
.Where(a => a.EmployeeId == summary.EmployeeId && a.Timestamp >= from && a.Timestamp< to)
.ToList();
var from=summary.Date;
var to=汇总日期添加天数(1);
var详细信息=Uow.EmployeeAttractions
.GetAllReadOnly()
其中(a=>a.EmployeeId==summary.EmployeeId&&a.Timestamp>=from&&a.Timestamp
作为使用DbFunctions.TruncateTime的替代方法,您只需执行以下操作:
var from = summary.Date;
var to = summary.Date.AddDays(1);
var details = Uow.EmployeeAttendances
.GetAllReadOnly()
.Where(a => a.EmployeeId == summary.EmployeeId && a.Timestamp >= from && a.Timestamp< to)
.ToList();
var from=summary.Date;
var to=汇总日期添加天数(1);
var详细信息=Uow.EmployeeAttractions
.GetAllReadOnly()
其中(a=>a.EmployeeId==summary.EmployeeId&&a.Timestamp>=from&&a.Timestamp
试试看
string f = summary.ToString("MM/dd/yyyy");
summary= Convert.ToDateTime(f); // time at 12:00 start date
var details = Uow.EmployeeAttendances.GetAllReadOnly()
.Where(a=>a.EmployeeId== summary.EmployeeId &&
a.Timestamp == summary).ToList();
试试看
您可以使用DateTime。比较 比如说
var details = Uow.EmployeeAttendances.GetAllReadOnly()
.Where(a => a.EmployeeId == summary.EmployeeId && DateTime.Compare(x.Timestamp .Date, summary.Date) == 0).ToList();
或者使用实体函数
var details = Uow.EmployeeAttendances.GetAllReadOnly()
.Where(a => a.EmployeeId == summary.EmployeeId && EntityFunctions.TruncateTime(x.Timestamp)== EntityFunctions.TruncateTime(summary.Date)).ToList();
您可以使用DateTime。比较 比如说
var details = Uow.EmployeeAttendances.GetAllReadOnly()
.Where(a => a.EmployeeId == summary.EmployeeId && DateTime.Compare(x.Timestamp .Date, summary.Date) == 0).ToList();
或者使用实体函数
var details = Uow.EmployeeAttendances.GetAllReadOnly()
.Where(a => a.EmployeeId == summary.EmployeeId && EntityFunctions.TruncateTime(x.Timestamp)== EntityFunctions.TruncateTime(summary.Date)).ToList();
这在Linq to实体中不起作用(如问题中所述)。这在Linq to实体中不起作用(如问题中所述)。有比您更好的答案,因为它们格式良好。因此,请考虑编辑您的问题,以适当的格式。这就是
代码片段的作用,代码显示为代码,而不仅仅是纯黑白的无格式文本。有比你更好的答案,因为它们格式良好。因此,请考虑编辑您的问题,以适当的格式。这就是代码片段的作用,代码将显示为代码,而不仅仅是纯黑白无格式文本。