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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:23:56+00:00 2026-05-18T00:23:56+00:00

my LINQ query returns an ordered sequence of calendar dates, and i need to

  • 0

my LINQ query returns an ordered sequence of calendar dates, and i need to output this sequence starting from the earliest date that is more that the given number of days apart from the starting date of the sequence.

the code below does that using linear search. it seems that i could use a binary search to find the beginning date if LINQ query supported this.

in this contrived example i can search the list but in my real code i am trying to avoid storing the whole sequence in memory and i prefer to use just IEnumerable.

any ideas how to make it more efficient? i have thousands of items in my query and doing linear search is just lame…

thanks
konstantin


using System;
using System.Collections.Generic;
using System.Linq;

namespace consapp
{
    static class Program
    {
        static void Main(string[] args)
        {
            var dates = new List<DateTime>();
            var xs = dates.OrderBy(x => x);

            dates.Add(DateTime.Parse("11/10/11"));
            dates.Add(DateTime.Parse("02/02/11"));
            dates.Add(DateTime.Parse("11/24/11"));
            dates.Add(DateTime.Parse("09/09/11"));
            dates.Add(DateTime.Parse("11/10/11"));

            var d = DateTime.MinValue;
            double offset = 1.2;

            foreach (var x in xs)
            {
                if (d != DateTime.MinValue)
                {
                    offset -= (x - d).Days;
                }
                if (offset < 1)
                {
                    Console.WriteLine(x.ToShortDateString());
                }
                d = x;
            }
        }
    }
}
  • 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-18T00:23:56+00:00Added an answer on May 18, 2026 at 12:23 am

    Binary search would likely be better if your data set is pre-sorted or you do not know the start date of your sequence ahead of time. However if you are sorting your dates using OrderBy like your example and you do know the start date of the sequence, why not put a Where clause in to filter out the dates that don’t meet your criteria before you order the sequence?

    var xs = from date in dates
             where (date - target).Days < 1.2
             order by date
             select date;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write a LINQ query that returns an hierachical anonymous type from
I've been trying to develop a linq query that returns the ItemNumber column of
I'm trying to run a LINQ to SQL query that returns a result in
This Linq to SQL query ... Return (From t In Db.Concessions Where t.Country =
Starting with the following LINQ query: from a in things where a.Id == b.Id
I have a linq query that for whatever reason is not coming back ordered
My linq query returns a collection of IEnumerable<object> . How can I convert this
I have a LINQ to EF query that returns data in a class form.
Can a Linq query retrieve BLOBs from a Sql Database? And how do they
I have a linq query and I am trying to put that in to

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.