public class Product {
public List<DateTime> FailureDates { get; set; }
}
I have a List<Product> and then I’m trying to do a grouping based on a List<DateTime> for use in a chart. The grouping is supposed to be based on the Year of the DateTime object. So I would want the end result to look something like:
Total Failures by Year
2012: 1,
2011: 5,
2010: 3
… and so on.
I originally had this working before the DateTime object became a list with the following code:
return Json.Encode(Products.Where(s => s.FailureDate != DateTime.MinValue).GroupBy(s => s.FailureDate.Year).Select(s => new { Year = s.Key.ToString(), FailureCount = s.Count() }).ToArray());
But, now that DateTime is a List so I’m not sure how to go about grouping it. I am thinking a merge needs to occur for this to work, so maybe the LINQ code will just end up being too messy and I would be better of using a non-LINQ solution (e.g. iterating through my products and then iterating through my dates and building out a dictionary to keep track of the counts).
This would work for you.
Or to use your example, just add a SelectMany on Products like this