I am basically trying to an "Archives" of dates grouped by Month and Year. However my problem is the dates in the database are strings...not my choice.
Here is my code to get that list of date groups
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();
But when I use it, I get this error:
LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)
How would I be able to accomplish what I am doing with string dates from the database?
If you know the format of your string then you can probably do something that looks like this :
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.