简体   繁体   English

ASP.NET实体框架LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)

[英]ASP.NET Entity Framework LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)'方法 - LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' method LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)' - LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' LINQ to Entities无法识别方法'System.DateTime ToDateTime(System.String)' - LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.String)' LINQ to Entities无法识别方法ToDateTime(System.String) - LINQ to Entities does not recognize the method ToDateTime(System.String) LINQ to Entities无法识别方法'System.DateTime ConvertShamsiToMiladi(System.String)'方法 - LINQ to Entities does not recognize the method 'System.DateTime ConvertShamsiToMiladi(System.String)' method LINQ to Entities无法识别MVC5中的方法'System.DateTime ParseExact(System.String,System.String,System.IFormatProvider)'方法错误 - LINQ to Entities does not recognize the method 'System.DateTime ParseExact(System.String, System.String, System.IFormatProvider)' method error in MVC5 LINQ to Entities 无法识别方法“System.DateTime GetValueOrDefault()” - LINQ to Entities does not recognize the method 'System.DateTime GetValueOrDefault()' LINQ to Entities 无法识别“System.String Decrypt(System.String, System.String)”方法 - LINQ to Entities does not recognize the method 'System.String Decrypt(System.String, System.String)' method LINQ to Entities无法识别方法'System.String getFullname(System.String,System.String)' - LINQ to Entities does not recognize the method 'System.String getFullname(System.String, System.String)' LINQ 到实体 - LINQ 到实体无法识别方法“双重解析(System.String)”方法 - LINQ To Entity - LINQ to Entities does not recognize the method 'Double Parse(System.String)' method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM