Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7829655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:52:02+00:00 2026-06-02T10:52:02+00:00

public class Product { public List<DateTime> FailureDates { get; set; } } I have

  • 0
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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:52:06+00:00Added an answer on June 2, 2026 at 10:52 am

    This would work for you.

    var query = from p in Products
                from f in p.FailureDates 
                where f != DateTime.MinValue
                group f by f.Year into g
                select new
                {
                  Year = g.Key.ToString(),
                  FailureCount = g.Count(),
                }
    

    Or to use your example, just add a SelectMany on Products like this

    return Json.Encode(Products.SelectMany(p => p.FailureDates)
                               .Where(s => s != DateTime.MinValue)
                               .GroupBy(s => s.Year)
                               .Select(s => new { Year = s.Key.ToString(), FailureCount = s.Count() }).ToArray());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have public class Product: Entity { public IList<Item> Items { get; set;
I have the following .Net class: public class Product { public int ID {get;set;}
I have one webservice: public class Product { public int ProductId { get; set;
I have this entity: public class Product { public string Name { get; set;
If I have the following model; public List<RecommendedProduct> recommendations Then public class RecommendedProduct public
I have this model: public class Package { public string CustomerName { get; set;
I have the object, which contain the list of objects. public class Product: IProduct
I have a class Product with a set of properties: public class Product {
Given the following object: public class Product { string Name {get;} int Quantity {get;}
Simple question on best practice. Say I have: public class Product { public string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.