kknaguib
kknaguib

Reputation: 749

Find the closest datetime to desired datetime and loop through list in reverse

I have a list of data that I've retrieved and is ordered from previous to more recent datetime. I'm trying to loop backwards in this list based on a time and date I specify, however the date and time in the list won't necessarily be equal to the datetime I specify so I want it to find the closest datetime in the list then iterate backwards. Here's an example:

I want "3/14/2014 8:35:33 AM"

List:

I tried doing this, but I'm really lost I don't know where to go from here:

foreach(var item in providerQuoteInfo)
{
   for(int i = providerQuoteInfo.Count; i >= 0; i--)
   {
   }
}

And providerQuoteInfo is the list of type ProviderQuote.

public class ProviderQuote
{
    public DateTime TimeStamp { get; set; }
    // ...
}

So now I want it to start at "3/14/2014 8:33:03 AM" and iterate up the list. How can I do something like that?

Upvotes: 0

Views: 228

Answers (3)

woutervs
woutervs

Reputation: 1510

Something like this to find the closest => Order the list, find the first value that is smaller or equal, find the first value that is bigger or equal (if equal that's your value) if not from those two values take the difference as a timespan. The value with the smallest difference is the closest.

Pseudo code.

    var list = new List<DateTime>();
    list = list.OrderBy(x => x).ToList();
    var target = new DateTime(2014, 3, 14);
    var smaller = list.First(x => x <= target);
    var bigger = list.First(x => x >= target);
    if (smaller == target) Console.WriteLine("Target found");
    else
    {
        var differenceWithSmaller = target - smaller;
        var differenceWithBigger = bigger - target;
        if(differenceWithSmaller < differenceWithBigger) Console.WriteLine("Smaller is closest to target");
        else Console.WriteLine("Bigger is closest to target");
    }

Upvotes: 0

Wasif Hossain
Wasif Hossain

Reputation: 3940

You may just filter the sequence with Where() method and loop through like below:

DateTime inputDateTime = "YOUR DATETIME VALUE";

var desiredListProviderQuote = 
listProviderQuote
 .OrderBy(x => x.TimeStamp)
 .Where(x => x.TimeStamp.CompareTo(inputDateTime) <= 0)
 .OrderByDescending(x => x.TimeStamp)
 .ToList();

foreach (ProviderQuote providerQuote in desiredListProviderQuote)
{
    // DO WHAT YOU WANT
}

Upvotes: 1

JLRishe
JLRishe

Reputation: 101662

Assuming performance isn't a huge concern, this should do the trick:

// quotes is the list of ProviderQuotes
// reference is the DateTime being used for comparison
IEnumerable<ProviderQuote> toUse = quotes.TakeWhile(q => q.TimeStamp <= reference)
                                         .Reverse();
foreach (ProviderQuote item in toUse)
{
    // do something with item
}

If performance is important, you could consider doing some sort of binary search to find the index of the nearest item and then iterate backwards from that index, though I don't see that providing that much of a performance improvement.

Upvotes: 4

Related Questions