I am trying to render a chart from the following query:
Questionnaires created by month and format the month as mm/YYYY
My code:
public ActionResult QuestionnairesByMonth() {
var query = TrialDB.Questionnaires
.GroupBy(r => new {
Month = r.DateCreated.Month,
Year = r.DateCreated.Year,
})
.Select(group => new {
Date = string.Format("{0}/{1}", group.Key.Year, group.Key.Month),
Total = group.Count()
})
.ToList();
var chart = new Chart(400, 200)
.AddTitle("Questionarios creados por mes")
.DataBindTable(query, "Date")
.Write();
return null;
}
but I’m getting the following error :
LINQ to Entities does not recognize the method ‘System.String ToString(System.String)’ method, and this method cannot be translated into a store expression.
I suggest you fetch everything you need in a “simple” Select query which will go to the database, and then perform the formatting locally using
AsEnumerableto switch to an in-process query:Or leave it as a
DateTimewhen pulling it back from the database, as Stuart suggests:For more on
AsEnumerable(), read my Edulinq blog post on it.