![](/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.