C# Linq连接时间错误
我正在尝试使用表上的联接创建列表。我已经注释掉了不起作用的代码,但这正是我需要的代码。当我运行此命令时,我得到一个强制转换日期时间错误。我想知道我是否应该尝试这个连接来获取数据或运行这个结果的子查询,我不知道该怎么做 如果BeingSeen为null,那么我希望新对象中的BeingSeen属性为null 有没有关于如何正确填充“BeingSeen”房产的建议C# Linq连接时间错误,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我正在尝试使用表上的联接创建列表。我已经注释掉了不起作用的代码,但这正是我需要的代码。当我运行此命令时,我得到一个强制转换日期时间错误。我想知道我是否应该尝试这个连接来获取数据或运行这个结果的子查询,我不知道该怎么做 如果BeingSeen为null,那么我希望新对象中的BeingSeen属性为null 有没有关于如何正确填充“BeingSeen”房产的建议 object PatientsNotSeen; using (var db
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属性不可为空,则只需键入cast即可:-
BeingSeen = (DateTime?)beingSeen.TimeStarted ?? DateTime.MinValue
试试这个:
BeingSeen=BeingSeen.TimeStarted??DateTime.MinValue
@rahussingh BeingSeen=(DateTime?)BeingSeen.TimeStarted??DateTime.MinValue工作正常。如果你把它贴出来,我会接受你的回答