【发布时间】:2017-06-26 10:11:08
【问题描述】:
当我通过 linq 查询从该表中获取数据并使用时,我的表中有 DateTimeOffset 列
(from c in this.dbContext.SomeTable
where c.Id == someId
select new SomeModel()
{
Id = c.Id,
Name = c.Name,
StartDate = c.StartDate.DateTime // <-- problematic line
}
在我的选择中,我得到以下异常:
LINQ to Entities 不支持指定的类型成员“DateTime”
在查询中获取数据时是否可以将 DateTimeOffset 转换为 DateTime? 我在 DbFunctions 中没有看到任何功能。 我是否必须使用 DateTimeOffset 获取数据并执行:
data.StartDate = data.StartDate.DateTime
必须有更简单的解决方案
【问题讨论】:
-
不确定是否有更好的方法,但理论上您可以选择一个匿名对象,执行
ToList(强制获取结果),然后在您那里进行选择这表明您对 DateTime 的调用将在 LINQ to objects 而不是 LINQ to Entities 中。