![](/img/trans.png)
[英]LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method
[英]ASP.NET Entity Framework LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)
我基本上是在尝试按月和年分组的“归档”日期。 但是我的问题是数据库中的日期是字符串...不是我的选择。
这是我的代码以获取日期组列表
var dates = dbBlog.Data
.GroupBy(o => new
{
Month = Convert.ToDateTime(o.date).Month,
Year = Convert.ToDateTime(o.date).Year
})
.Select(g => new BlogArchiveClass
{
Month = g.Key.Month,
Year = g.Key.Year,
Total = g.Count()
})
.OrderByDescending(a => a.Year)
.ThenByDescending(a => a.Month)
.ToList();
但是,当我使用它时,会出现以下错误:
LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)
如何使用数据库中的字符串日期完成我的工作?
如果您知道字符串的格式,则可以执行以下操作:
var dates = dbBlog.Data
Select(d => new
{
Month = d.Month.Substring(....),
Year = d.Year.Substring(....)
})
.GroupBy(o => new { o.Month,o.Year})
.Select(g => new BlogArchiveClass
{
Month = g.Key.Month,
Year = g.Key.Year,
Total = g.Count()
})
.OrderByDescending(a => a.Year)
.ThenByDescending(a => a.Month)
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.