0

A controller should return data that is used as source for a D3.js line chart. In the controller, I've got a list of objects and I'm only interested in the property "Begin" and count(Begin). Begin is a datetime and what I want is to group all Objects where "Begin" is on the same day, and then count the number for each day.

I try to select this information and return it this way:

        var results = from a in db.Questionaires
                  group a by a.Begin.Date into g
                  select new { Date = g.Key, Count = g.Count() };

        return Json( results, JsonRequestBehavior.AllowGet);

Unfortunatly, I'm getting an error because the group by clause seems to be wrong ("The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.").

How to select the information the right way? If anybody has some examples on D3.js and MVC I would appreciate that.

1 답변


1

You should try this

var results = from a in db.Questionaires
                  group a by new { y = a.Begin.Year, m = a.Begin.Month, d = a.Begin.Day} 
                  into g
                  select new { Day = g.Key.d, Year = g.Key.y, 
                              Month = g.Key.m, Count = g.Count(),
                              Date = g.Select(d=>d.Begin).FirstOrDefault() };


  • Thanks! If I want to have as result only Date (dd.mm.yyyy) and Count, then how would I achieve that? Concatenating ( .. + "." + ..) did not work - peter
  • I am not sure you can directly do that in a linq query that will be executed in the database server. May be have a readonly property on the object which will be giving you a Date/DateTime object using those internal values. - AD.Net
  • I modified a bit, see if you can do that to get the datetime - AD.Net

Related

Latest