Linq To Sql仅比较时间

时间:2014-01-22 15:56:12

标签: c# sql linq

如何在不获取以下内容的情况下比较DateTime对象的时间

错误:

An exception of type 'System.NotSupportedException' occurred in mscorlib.dll but was not handled in user code Additional information: The specified type member 'TimeOfDay' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

我的代码:

var date = DateTime.Parse(query.DueTime);
entities = entities.Where(r => r.DueTime.TimeOfDay.Equals(date.TimeOfDay));

2 个答案:

答案 0 :(得分:16)

这就是答案!

var date = DateTime.Parse(query.DueTime);
var time = date.TimeOfDay;
entities = entities.Where(r => DbFunctions.CreateTime(r.DueTime.Hour, r.DueTime.Minute,  r.DueTime.Second) == time);

答案 1 :(得分:-3)

您是否尝试过使用EntityFunctions.TruncateTime(TIME)函数?

它将如下:

var date = DateTime.Parse(query.DueTime);
entities = entities.Where(r => EntityFunctions.TruncateTime(r.DueTime) == EntityFunctions.TruncateTime(date));

希望这有帮助!