public List<DateTime> CalorieDates(int patientId)
{
using (var db = new DbConn())
{
List<DateTime> query =
db.Calories.Where(d => d.PatientId == patientId && d.FoodId != "initial" && d.DateOfEntry != null)
.Select(d => System.Data.Entity.DbFunctions.TruncateTime(d.DateOfEntry)).Distinct().ToList();
return query;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么这会将我的列表转换为可以为空的日期?
Error CS0029 Cannot implicitly convert type
'System.Collections.Generic.List<System.DateTime?>'
to 'System.Collections.Generic.List<System.DateTime>'
Run Code Online (Sandbox Code Playgroud)
我该如何防止这种情况?我不想要一个可以为空的日期时间列表.
由于某些未知原因(可能使查询提供者生活更轻松),DbFunctions类不TruncateTime为DateTime和提供单独的重载DateTime?,但是带有DateTime?参数的单个方法可以处理这两种情况.但是,作为副作用,DateTime?即使参数是DateTime(因为它似乎在你的情况下),它也会改变表达式类型.
所以你需要纠正这个问题.一旦你知道你总是在路过DateTime(hense结果不可能null),你可以简单地.Value在TruncateTime通话结束时添加,问题就解决了.
.Select(d => System.Data.Entity.DbFunctions.TruncateTime(d.DateOfEntry).Value)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
291 次 |
| 最近记录: |