【发布时间】:2018-06-09 01:05:43
【问题描述】:
DateTime.ToString("MM/dd/yyyy") 里面 linq 抛出一个错误。这是我的代码:
public IEnumerable<EmployeeViewModel> GetAllEmployees()
{
List<EmployeeViewModel> vm = (
from a in db.Employees
select new EmployeeViewModel
{
EmployeeId = a.EmployeeId,
EmployeeName = a.EmployeeName,
HiredDateString = a.HiredDateTime.ToString("MM/dd/yyyy"),
HiredTimeString = a.HiredDateTime.ToString("h:mm tt"),
Description= a.Description
}).ToList();
return vm;
}
编辑:HiredDateString 和 HiredTimeString 是 ViewModel 中的字符串。
为此我收到以下错误:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
LINQ to Entities does not recognize the method 'System.String ToString(System.String)' method, and this method cannot be translated into a store expression.
</ExceptionMessage>
<ExceptionType>System.NotSupportedException</ExceptionType>
如果我简单地使用:a.HiredDateTime.ToString(),它不会抛出错误。但它给了我完整的 DateTime 字符串;我想分别获取日期和时间字符串。 非常感谢您的帮助。
【问题讨论】:
-
a.HiredDateTime是DateTime还是string?看起来可能是string -
为什么要将日期存储为字符串?这总是坏主意。如果您想以某种方式显示它们,请在视图层中进行
-
@BobKaufman 这是一个字符串
标签: c# entity-framework linq