I have a list of objects like this:
CreateDate Name Unit
2015-01-01 Users 100
2015-01-01 Items 50
2015-01-02 Users 400
2015-01-02 Items 150
Now i want to create a json array where i group the items in the list by CreateDate and get this:
[
{"CreateDate":"2015-01-01", "Users":100, "Items" : 50},
{"CreateDate":"2015-01-02", "Users":400, "Items" : 150}
]
CreateDate and Name is PK in the database and contains more type of names.
I have this:
public class Statistics
{
public DateTime CreateDate { get; set; }
public int Unit { get; set; }
public string Name { get; set; }
}
[HttpGet]
public ActionResult Items(DateTime startDate, DateTime endDate)
{
try
{
List<Statistics> Coll = FROM DB
var Data = Coll.GroupBy(p => p.CreateDate);
return Json(Coll, JsonRequestBehavior.AllowGet);
}
catch (Exception Ex)
{
return ...
}
}
What is the best way to do this?
Datavariable? Does your code work and you're just looking for a better way? Or are you getting some error?GroupBydon't modify the sourceIEnumerable