Linq 加入时间错误
本文关键字:错误 时间 Linq | 更新日期: 2023-09-27 18:33:28
我正在尝试使用表上的连接创建一个列表。我已经注释掉了不起作用的代码,但这是我需要的代码。当我运行它时,我收到一个投射日期时间错误。我想知道我是否应该尝试这个联接来获取数据或运行这个结果的子查询,我不知道该怎么做。
如果 BeingSeen为空,那么我希望新对象中的 BeingSeen 属性为空。
关于填写这个"被看见"属性的正确方法有什么建议吗?
object PatientsNotSeen;
using (var db = new SyDbConn())
{
PatientsNotSeen = (from events in db.Events
where
events.ClinicId == clinicId && events.StatusId != 6 &&
DbFunctions.TruncateTime(events.stime) == DbFunctions.TruncateTime(DateTime.Now)
//join beingSeen in db.BeingSeenStatus on events.PatientId equals beingSeen.PatientId into caa
//from beingSeen in caa.DefaultIfEmpty()
select new
{
PatientID = events.PatientId,
events.PatientName,
AppointmentTime = events.stime,
// BeingSeen = beingSeen.TimeStarted
}).ToList();
}
取消注释注释并运行时,这是错误:
Additional information: The cast to value type
'System.DateTime' failed because the materialized value is null. Either the result type's generic
parameter or the query must use a nullable type.
您可以使用
Null Coalesce
运算符:-
BeingSeen = beingSeen.TimeStarted ?? DateTime.MinValue
如果 TimeStarted 属性不是Nullable
则只需对其进行类型转换:-
BeingSeen = (DateTime?)beingSeen.TimeStarted ?? DateTime.MinValue