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

  • SEARCH
  • Home
  • 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 7729757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:03:10+00:00 2026-06-01T06:03:10+00:00

I have an IEnumerable<DateTime> . I would like to pull out all dates that

  • 0

I have an IEnumerable<DateTime>. I would like to pull out all dates that occur in the current calendar month even if they have passed as well as all dates in the next 11 calendar months. I’ll be displaying the dates grouped by month starting with the most recent. For example:

Current Date – Mar 12 2012

  • March 2012
    • March 3 – Event 1
    • March 15 – Event 2
    • March 30 – Event 3
  • …
  • …
  • …
  • February 2013
    • Feb 3 – Event 156
    • Feb 13 – Event 157
    • Feb 20 – Event 158

I already know how to do the grouping part. How would you accomplish filtering the dates using linq and C#?

  • 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-01T06:03:11+00:00Added an answer on June 1, 2026 at 6:03 am

    Something like this should work. You first determine the legal range of dates, then filter out any dates that are outside of that range, and finally you group them by month.

    I wrote the following code by hand, you may need to tweak it:

    var acceptableFirstDate = DateTime.Today;
    if (DateTime.Today.Day > 1)
    {
        acceptableFirstDate = DateTime.Today.AddDays(-DateTime.Today.Day + 1);
    }
    var acceptableLastDate = acceptableFirstDate.AddMonths(12).AddDays(-1);
    
    var result = dates
        .Where(x => x >= acceptableFirstDate && x <= acceptableLastDate)
        .GroupBy(x => x.Month);
    

    I hope this helps!

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

Sidebar

Related Questions

I have an IEnumerable object. I would like to access based on index for
All of my tables have common audit fields: modifiedBy,modifiedDateTime, etc. I would like to
I have an IEnumerable that I've materialized from Linq2Sql. I've already filtered out the
I have an IEnumerable<DirectoryInfo> that I want to filter down using an array of
I have an IEnumerable< T> which is declared on the page like this: IEnumerable<
I have an IEnumerable, listOfOnes, and an IEnumerable, listOfTwos. Assuming that I can compare
I have a SearchService with a Search() method that retrieves an IEnumerable<Search> from a
I have an entity set of Publications with a ReleaseDate property. I would like
I have a method that gets a list of objects and then returns all
I have an IEnumerable full of objects that we are using to represent user

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.