简体   繁体   English

LINQ to Entities无法识别方法'System.String GetMonthName(Int32)'方法

[英]LINQ to Entities does not recognize the method 'System.String GetMonthName(Int32)' method

What am I doing wrong? 我究竟做错了什么? FYI The repository method GetResearchArticles() returns an IQueryable. 仅供参考,存储库方法GetResearchArticles()返回一个IQueryable。

var grouped = (from p in _researchArticleRepository.GetResearchArticles()
               group p by new { month = p.DatePublished.Month, year = p.DatePublished.Year } into d
               select new
               {
                   dt = d.Key.month + "-" + d.Key.year,
                   dtByMonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(d.Key.month) + " " + d.Key.year
               }//count = d.Count() }
               )
               .OrderByDescending(g => g.dt);
return grouped.ToDictionary(item => item.dt, item => item.dtByMonthName);

It is probably trying to convert that statement to a SQL expression, which it cannot do. 它可能正在尝试将该语句转换为SQL表达式,这是做不到的。 Instead of trying to do that on the database, you should make that call on data that has already been retrieved from the database. 与其尝试在数据库上执行此操作,不如对已从数据库中检索到的数据进行调用。

Basically, you need to run .ToList() or something to force the fetching of the data, and then make a call to GetMonthName() 基本上,您需要运行.ToList()或强制执行数据获取操作,然后调用GetMonthName()

since your not doing any filtering this should work but your pulling out all research articles into memory because SQL doesnt understand how to get month 由于您不进行任何过滤,这应该可以工作,但是您将所有研究文章都拉到了内存中,因为SQL无法理解如何获取月份

var grouped = (from p in _researchArticleRepository.GetResearchArticles().ToList()
                   group p by new { month = p.DatePublished.Month, year = p.DatePublished.Year } into d
                   select new
                   {
                       dt = d.Key.month + "-" + d.Key.year,
                       dtByMonthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(d.Key.month) + " " + d.Key.year
                   }//count = d.Count() }
                   )
                   .OrderByDescending(g => g.dt);
    return grouped.ToDictionary(item => item.dt, item => item.dtByMonthName);

暂无
暂无

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

相关问题 LINQ to Entities无法识别方法'Int32 ToInt32(System.String)'方法 - LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method LINQ to Entities无法识别方法'Int32 IndexOf(System.String,System.StringComparison)'方法 - LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String, System.StringComparison)' method LINQ to Entities 无法识别方法 'Int32 Int32(System.String)' 方法,并且此方法无法转换为存储表达式 - LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression LINQ 到 Entities 无法识别方法 'System.String SetAssetStatus(Int32)' 方法 - LINQ to Entities does not recognize the method 'System.String SetAssetStatus(Int32)' method LINQ to Entities无法识别方法'Int32 CompareTo(System.String)'方法 - LINQ to Entities does not recognize the method 'Int32 CompareTo(System.String)' method LINQ to Entities 无法识别“System.String get_Item(Int32)”方法 - LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method LINQ to Entities无法识别方法'System.String ToString(Int32)'方法 - LINQ to Entities does not recognize the method 'System.String ToString(Int32)' method LINQ to Entities无法识别方法'Int32 Parse(System.String)'方法, - LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, LINQ to Entities无法识别方法'Int32 Parse(System.String)'为什么? - LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' why? LINQ to Entities无法识别方法'System.String ToString(Int32)' - LINQ to Entities does not recognize the method 'System.String ToString(Int32)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM