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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:56:36+00:00 2026-05-15T01:56:36+00:00

I’m a complete LINQ newbie, so I don’t know if my LINQ is incorrect

  • 0

I’m a complete LINQ newbie, so I don’t know if my LINQ is incorrect for what I need to do or if my expectations of performance are too high.

I’ve got a SortedList of objects, keyed by int; SortedList as opposed to SortedDictionary because I’ll be populating the collection with pre-sorted data. My task is to find either the exact key or, if there is no exact key, the one with the next higher value. If the search is too high for the list (e.g. highest key is 100, but search for 105), return null.

// The structure of this class is unimportant.  Just using
// it as an illustration.
public class CX
{
    public int KEY;
    public DateTime DT;
}

static CX getItem(int i, SortedList<int, CX> list)
{
    var items =
    (from kv in list
     where kv.Key >= i
     select kv.Key);

    if (items.Any())
    {
        return list[items.Min()];
    }

    return null;
}

Given a list of 50,000 records, calling getItem 500 times takes about a second and a half. Calling it 50,000 times takes over 2 minutes. This performance seems very poor. Is my LINQ bad? Am I expecting too much? Should I be rolling my own binary search function?

  • 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-15T01:56:37+00:00Added an answer on May 15, 2026 at 1:56 am

    Writing a binary search on your own can be tough.

    Fortunately, Microsoft already wrote a pretty robust one: Array.BinarySearch<T>. This is, in fact, the method that SortedList<TKey, TValue>.IndexOfKey uses internally. Only problem is, it takes a T[] argument, instead of any IList<T> (like SortedList<TKey, TValue>.Keys).

    You know what, though? There’s this great tool called Reflector that lets you look at .NET source code…

    Check it out: a generic BinarySearch extension method on IList<T>, taken straight from the reflected code of Microsoft’s Array.BinarySearch<T> implementation.

    public static int BinarySearch<T>(this IList<T> list, int index, int length, T value, IComparer<T> comparer) {
        if (list == null)
            throw new ArgumentNullException("list");
        else if (index < 0 || length < 0)
            throw new ArgumentOutOfRangeException((index < 0) ? "index" : "length");
        else if (list.Count - index < length)
            throw new ArgumentException();
    
        int lower = index;
        int upper = (index + length) - 1;
    
        while (lower <= upper) {
            int adjustedIndex = lower + ((upper - lower) >> 1);
            int comparison = comparer.Compare(list[adjustedIndex], value);
            if (comparison == 0)
                return adjustedIndex;
            else if (comparison < 0)
                lower = adjustedIndex + 1;
            else
                upper = adjustedIndex - 1;
        }
    
        return ~lower;
    }
    
    public static int BinarySearch<T>(this IList<T> list, T value, IComparer<T> comparer) {
        return list.BinarySearch(0, list.Count, value, comparer);
    }
    
    public static int BinarySearch<T>(this IList<T> list, T value) where T : IComparable<T> {
        return list.BinarySearch(value, Comparer<T>.Default);
    }
    

    This will let you call list.Keys.BinarySearch and get the negative bitwise complement of the index you want in case the desired key isn’t found (the below is taken basically straight from tzaman’s answer):

    int index = list.Keys.BinarySearch(i);
    if (index < 0)
        index = ~index;
    var item = index < list.Count ? list[list.Keys[index]] : null;
    return item;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I need a function that will clean a strings' special characters. I do NOT

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.