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 702787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:45:33+00:00 2026-05-14T03:45:33+00:00

I have a list of objects. Each object has an integer quantity and a

  • 0

I have a list of objects. Each object has an integer quantity and a DateTime variable which contains a month and year value. I’d like to traverse the list and pad the list by adding missing months (with quantity 0) so that all consecutive months are represented in the list. What would be the best way to accomplish this?

Example:
Original List

{ Jan10, 3 }, { Feb10, 4 }, { Apr10, 2 }, { May10, 2 }, { Aug10, 3 }, { Sep10, -3 }, { Oct10, 6 }, { Nov10, 3 }, { Dec10, 7 }, { Feb11, 3 }

New List

{ Jan10, 3 }, { Feb10, 4 }, {Mar10, 0}, { Apr10, 2 }, { May10, 2 }, { Jun10, 0 }, { Jul10, 0 } { Aug10, 3 }, { Sep10, -3 }, { Oct10, 6 }, { Nov10, 3 }, { Dec10, 7 }, { Jan11, 0 }, { Feb11, 3 }

  • 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-05-14T03:45:34+00:00Added an answer on May 14, 2026 at 3:45 am

    One possible algorithm is to keep track of the previous and current months. If the difference between previous and current is 1 month, append current to the result. If the difference is more than one month, add the missing months first, then afterwards copy the current month.

    Foo prev = months.First();
    List<Foo> result = new List<Foo> { prev };
    foreach (Foo foo in months.Skip(1))
    {
        DateTime month = prev.Month;
        while (true)
        {
            month = month.AddMonths(1);
            if (month >= foo.Month)
            {
                break;
            }
            result.Add(new Foo { Month = month, Count = 0 });
        }
        result.Add(foo);
        prev = foo;
    }
    

    Results:

    01-01-2010 00:00:00: 3
    01-02-2010 00:00:00: 4
    01-03-2010 00:00:00: 0
    01-04-2010 00:00:00: 2
    01-05-2010 00:00:00: 2
    01-06-2010 00:00:00: 0
    01-07-2010 00:00:00: 0
    01-08-2010 00:00:00: 3
    01-09-2010 00:00:00: -3
    01-10-2010 00:00:00: 6
    01-11-2010 00:00:00: 3
    01-12-2010 00:00:00: 7
    01-01-2011 00:00:00: 0
    01-02-2011 00:00:00: 3
    

    Other code needed to make this compile:

    class Foo
    {
        public DateTime Month { get; set; }
        public int Count { get; set; }
    }
    
    List<Foo> months = new List<Foo>
    {
        new Foo{ Month = new DateTime(2010, 1, 1), Count = 3 },
        new Foo{ Month = new DateTime(2010, 2, 1), Count = 4 },
        new Foo{ Month = new DateTime(2010, 4, 1), Count = 2 },
        new Foo{ Month = new DateTime(2010, 5, 1), Count = 2 },
        new Foo{ Month = new DateTime(2010, 8, 1), Count = 3 },
        new Foo{ Month = new DateTime(2010, 9, 1), Count = -3 },
        new Foo{ Month = new DateTime(2010, 10, 1), Count = 6 },
        new Foo{ Month = new DateTime(2010, 11, 1), Count = 3 },
        new Foo{ Month = new DateTime(2010, 12, 1), Count = 7 },
        new Foo{ Month = new DateTime(2011, 2, 1), Count = 3 }
    };
    

    Note: For simplicity I haven’t handled the case where the original list is empty but you should do this in production code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of objects: [Object_1, Object_2, Object_3] Each object has an attribute:
Background I have a list . This list has many objects. Each object has
I have a list of objects, and each object has a string property. For
Let's say I have an array list of objects, and each object has the
As input I have list of all objects where each object has properties: name
I have a list of objects representing widgets. Each widget object has a manufactured
I have a List with objects. Each object has an ID. I want to
I have a list of objects, where each object has a boolean property named
I have a list of objects (clusters) and each object has an attribute vertices
I have a list of order objects. Each order object has 5 instance variables.

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.