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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:17:35+00:00 2026-05-11T18:17:35+00:00

I have a small problem where I want to find the next active item

  • 0

I have a small problem where I want to find the next “active” item in a list with linq. Then next “active” item is defined by a startDate and EndDate. Here is an example list.

    //-- Create Lists of turns    
    IList<Turn> turns= new List<Turn>(){ 
                new Turn(){Name = "Turn 1", StartDate = DateTime.Parse("2009-05-01"), EndDate = DateTime.Parse("2009-05-01") }
         ,      new Turn(){Name = "Turn 2", StartDate = DateTime.Parse("2009-06-01"), EndDate = DateTime.Parse("2009-06-01") }
         ,      new Turn(){Name = "Turn 3", StartDate = DateTime.Parse("2009-07-01"), EndDate = DateTime.Parse("2009-07-02") }
         ,      new Turn(){Name = "Turn 4", StartDate = DateTime.Parse("2009-08-01"), EndDate = DateTime.Parse("2009-08-03") }
    }

//-- Get the next Turn by DateTime.
DateTime toDay = DateTime.Parse("2009-06-02");

//-- Should return the "Turn 3" item...
Turn turn = (from item in turns
            where .....
            select turn).FirstOrDefault<Turn>();

Is there a good solution to find the next turn by using startDate/endDate properties on Turn. I have tryed to first order the list by startdate and the find First one in the list, but I wounder if there is a more “safe” way to get it that dosen’t need the list in correct order to find the correct Turn.

  • 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-11T18:17:36+00:00Added an answer on May 11, 2026 at 6:17 pm

    Why don’t you just get the first item with a start date after today? I added an explict OrderBy() call just to be sure that the list is sorted. If you know that it is sorted, you can leave that out, of course.

    turns.OrderBy(t => t.StartDate).FirstOrDefault(t => t.StartDate > today);
    

    UPDATE

    I missed your last lines. Yes, you can do it without sorting the list explicitly. You have to search the list for the item with a start date after today and ther must be no item with a start date after today but before that of the current item. But this will really slow down the search because you have to look at the whole list for every item making the thing O(n²).

    turns.Single(t => 
        t.StartDate > today &&
        turns.All(u => u.StartDate <= today || u.StartDate > t.StartDate))
    

    This assumes that the turns are non-overlapping. If they overlapp, you must use First instead of Single or add additional constraints to get a unique result. And finally: use the the sorting solution – this solution is by no means safer than sorting and getting the first item.

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

Sidebar

Related Questions

I have a small problem and I do not find any solutions. I want
I have a small problem, looked here & other forums, but could not find
i have a small problem, i want to find in <tr><td>3</td><td>foo</td><td>2</td> the foo, i
I have small problem with my .net 2.0 winforms application. I want to embed
I have a small problem with a script. I want to have a default
I have a small problem with how should i think a... problem. I want
I have a small problem with interfaces. Here it is in Pseudo code :
I have a small problem :) I was searching the web but didn't find
SQL to find duplicate entries (within a group) I have a small problem and
Here is the problem: I have many sets of points, and want to come

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.