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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:08:16+00:00 2026-05-25T23:08:16+00:00

I am using LINQ to Objects and wonder if it is possible to improve

  • 0

I am using LINQ to Objects and wonder if it is possible to improve the performance of my queries by making use of an index that I have. This is best explained with an example. Imagine a simple type…

public class Person
{
    public int Age;
    public string FirstName;
    public string LastName;
}

And a simple query I would make against it…

List<Person> people = new List<Person>();

// 'people' populated with 50,000 instances...

var x = from t in people
        where t.Age > 18 && t.Age < 21
        select t;

If I understand LINQ to Objects correctly then the implementation of the Where extension method will enumerate all 50,000 instances in the people collection in order to find the 100 that actually match. As it happens I already have an index of the people collection that is sorted by Age. Like this…

SortedList<int, Person> ageSorted = new SortedList<int, Person>();

Clearly it would make sense if I could get the Where to use the SortedList so that it no longer has to enumerate all 50,000 instances, instead finding the range of 100 matching entries and so saving time.

Is it possible to extend LINQ to Objects to enable my situation? Is it already possible but I am missing the technique?

  • 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-25T23:08:16+00:00Added an answer on May 25, 2026 at 11:08 pm

    There’s already a project which I believe does exactly that – i4o. I can’t say I’ve used it myself, but it sounds like the kind of thing you want… you may need to juggle your existing code a bit, but it’s certainly worth looking at.

    If that doesn’t help, you could at least write your own extension methods on SortedList<TKey, TValue>. You probably wouldn’t be able to easily use your actual where clause, but you could use your own methods taking a minimum and a maximum value. You might also want to make them apply to IList<T> where you assert that you’ve already sorted the values appropriately (according to some comparer).

    For example (completely untested):

    public static IEnumerable<T> Between<T, TKey>(this IList<T> source,
                                                  Func<T, TKey> projection,
                                                  TKey minKeyInclusive,
                                                  TKey maxKeyExclusive,
                                                  IComparer<TKey> comparer)
    {
        comparer = comparer ?? Comparer<TKey>.Default;
    
        // TODO: Find the index of the lower bound via a binary search :)
        // (It's too late for me to jot it down tonight :)
        int index = ...; // Find minimum index
    
        while (index < source.Count &&
               comparer.Compare(projection(source[index]), maxKeyExclusive) < 0)
        {
            yield return source[index];
            index++;
        }
    }
    

    (If you only have List<T> instead of IList<T>, you could use List<T>.BinarySearch, although you’d need to build a custom IComparer<T>.)

    Also, have a look at SortedSet<T> in .NET 4.

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

Sidebar

Related Questions

I have a question regarding the integration of business objects developed using Linq To
I'm using linq to pull back an object (i.e. customer) that might have a
I noticed that LINQ to Objects has a GroupBy method. In light of this,
I have been using LINQ to query my POCO objects for some time, but
I am using LINQ to Entites where I have two table objects ( ReferenceCodes
Possible Duplicates: Sorting a list using Lambda/Linq to objects C# List<> OrderBy Alphabetical Order
I have an issue while trying to parse an Xml to Objects using Linq
We have a website which is using linq to entities, we found that it's
What is the best way to do a conditional query using linq to objects(not
Wondering if it's possible to create a dynamic linq query using linq to objects.

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.