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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:13:08+00:00 2026-06-07T13:13:08+00:00

I have a list of objects that contain a start/end date. I am trying

  • 0

I have a list of objects that contain a start/end date. I am trying to either find the current one if a current record exists, or failing that, the most recently expired one.

  • Current is defined as a non null start date that is in the past and either a null end date (open ended) or and end date in the future.
  • The most recently expired could be defined as having no current time period with a maximum end date. In theory, the time spans denoted by each object should never overlap so I’d expect maximum end date to be sufficient for most recently expired.

I was trying to follow this answer to a similar question, but I can’t seem to find the correct entry when there are other entries in the list that are future dated (ie. have a start/end date where both are in the future).

My attempt is as follows where a is start date, b is end date, c is an indicator for me to use while debugging, and d is used to define a list entry type. I am only interested in type 2.

   [TestMethod]
    public void TestRetrievalOfListElements()
    {

        var baseDate = DateTime.Now;

        var list = new[]
        {
             new { a = new DateTime?(baseDate.AddDays(-90)), 
                   b = new DateTime?(baseDate.AddDays(-60)), c = "LongExpired", d=1 },
             new { a = new DateTime?(baseDate.AddDays(-190)), 
                   b = new DateTime?(baseDate.AddDays(-160)), c = "LongestExpired", d=1 },
             new { a = new DateTime?(baseDate.AddDays(-59)), 
                   b = new DateTime?(baseDate.AddDays(+20)), c = "Current", d=1 },
             new { a = new DateTime?(baseDate.AddDays(-159)), 
                   b = new DateTime?(baseDate.AddDays(-91)), c = "LongerExpired", d=1 },
             new { a = new DateTime?(baseDate.AddDays(-90)), 
                   b = new DateTime?(baseDate.AddDays(-60)), c = "LongExpired", d=2 },
             new { a = new DateTime?(baseDate.AddDays(-190)), 
                   b = new DateTime?(baseDate.AddDays(-160)), c = "LongestExpired", d=2 },
             new { a = new DateTime?(baseDate.AddDays(-59)), 
                   b = new DateTime?(baseDate.AddDays(+20)), c = "Current", d=2 },
             new { a = new DateTime?(baseDate.AddDays(-159)), 
                   b = new DateTime?(baseDate.AddDays(-91)), c = "LongerExpired", d=2 },
             new { a = new DateTime?(baseDate.AddDays(+21)), 
                   b = new DateTime?(baseDate.AddDays(+60)), c = "Future", d=2 },

        }.ToList();

        // The following isn't really right either as it doesn't take into account
        // whether d is of type 1 or 2 either. Not sure how to combine aggregates and
        // other conditions ie MIN and of type 2

        var oldestEntry = (from x in list
                          where x.a == list.Min(d => d.a) &&
                          x.d == 2
                          select x).FirstOrDefault();

        Assert.IsTrue(oldestEntry.a == baseDate.AddDays(-190), "Expected oldest date to be 190 days earlier than today");

        var latestEntryThatIsntInTheFuture = 
                          (from x in list
                          from y in list.Where(z => z.d == 2 && z.a <= baseDate)
                          where x.a == list.Max(d => d.a) &&
                          x.d == 2
                          select x).FirstOrDefault();

        Assert.IsTrue(latestEntryThatIsntInTheFuture.a == baseDate.AddDays(-59), "Expected latest date that isn't in the future to be 59 days earlier than today");



    }
  • 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-07T13:13:11+00:00Added an answer on June 7, 2026 at 1:13 pm

    This might be what you’re looking for:

    // Find the current which is defined as a non null start date that is in the past 
    // and either a null end date (open ended) or and end date in the future. 
    var now = DateTime.Now;
    var current = list
        .Where(x => x.a.HasValue && x.a.Value < now && (!x.b.HasValue || x.b.Value > now))
        .FirstOrDefault();
    if (current == null)
    { 
        //Then find most recently expired 
        current = list.Where(x => x.b.HasValue)
                      .OrderByDescending(x => x.b)
                      .FirstOrDefault();
    }
    

    So you don’t need an aggregate in the query in my opinion.

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

Sidebar

Related Questions

I have a listview that is loaded with a list of objects that contain
I have two datetime objects; a start date and an end date. I need
I have a class Player that contains a list of Accessory objects. There are
I have a Ticket class that contains a List of User objects: class Ticket
I have a list of objects that have two int properties. The list is
I have a List of objects that I would like to convert to a
I have a List of String objects that are pipe delimited. something like this:
I have a list/collection of objects that may or may not have the same
I have a list of pointers that reference time objects that need a turn
I have a list of IDs for objects that I need to grab, then

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.