我正在开发一个MVC网站。 在其中一种视图中,我需要使用:
return View(db.Logs
.OrderByDescending(l => l.Date)
.Where(l => ((DateTime)l.Date).ToLocalTime().ToString().Contains(search)
|| l.Name.Contains(search))
.ToList()
.ToPagedList(pageNumber, pageSize));
但是我得到这个错误:
LINQ to Entities does not recognize the method 'System.DateTime
ToLocalTime()' method, and this method cannot be translated into a
store expression.
我需要将l.Date转换为LocalTime,然后与search进行比较
我该怎么做?
因为错误说ToLocaltime()在Linq中不支持实体。您需要在linq to Entities操作之后执行该转换
您是否尝试过((((DateTime)l.Date).ToLocalTime()。ToString())。Contains(search)?
您无法在Linq上使用强制类型转换,请尝试在使用属性类型相同的操作之前声明一个变量
而不是尝试将表行值转换为LocalTime,而是尝试将搜索条件转换为UTC日期时间。这可能相似
@ user9405863我该怎么办?
您应该将日志具体化为一个列表:varorderedLogs &#