[英]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.