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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:06:45+00:00 2026-06-18T07:06:45+00:00

I have a variable List< Tuple< DateTime, double>> myList . Given a datetime ,

  • 0

I have a variable List< Tuple< DateTime, double>> myList.
Given a datetime, hope it returns the Tuple that precedes
the datetime by using Linq.
For example, if "2013-Feb-08 21:34:00" is supplied, want to
see the last Tuple in the list whose datetime is before this timestamp.

How do I do this with Linq?

Edit:
myList.Where(t => t.Item1 < timestamp).Last();
solved my issue.
Which is better in terms of performance compared to
myList.TakeWhile(t => t.Item1 < timestamp).Last();

  • 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-18T07:06:46+00:00Added an answer on June 18, 2026 at 7:06 am

    With MoreLinq MaxBy (available from NuGet):

    myList.Where(t => t.Item1 < timestamp).MaxBy(t => t.Item1);
    

    Or (if items are sorted):

    myList.TakeWhile(t => t.Item1 < timestamp).Last();
    

    UPDATE (with binary search) write comparer:

    public class MyComparer : IComparer<Tuple<DateTime, double>>
    {
        public int Compare(Tuple<DateTime, double> x, Tuple<DateTime, double> y)
        {
            return x.Item1.CompareTo(y.Item1);
        }
    }
    

    Then search

       int index = myList.BinarySearch(new Tuple<DateTime, double>(timestamp, 0), 
                                       new MyComparer());
    
       if (index == 0)
          // there is no items before timestamp
    
       if (index > 0)
          result = myList[index - 1]; // your item is previous
    
       if (index < 0) // no tuple with date equal to timestamp
          var nearestIndex = ~index;
          if (nearestIndex > 0)
              result = myList[nearestIndex - 1];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a variable like that: List<Double> frameList = new ArrayList<Double>(); /* Double elements
I have a variable that can either contain a list of strings or a
I have a list of cities names that is variable and I want to
I have a list of class object that I created as a variable in
I have code that generates a List<string[]> variable but can't quite figure out how
I have a variable x (list)that looks like ['JACKIE:34', 'MATT:444', 'CEN:12', 'PETE:12', 'RANDY:92', 'MITCH:2',
I'm puzzled on how to expand List/Seq/Array into variable-length argument list. Given that I
If I have variable of type IEnumerable<List<string>> is there a LINQ statement or lambda
I have the following code, the problem is that my variable list @LocationList is
I have a list of dictionaries that have variable depth and different keys. for

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.