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

  • Home
  • SEARCH
  • 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 8984079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:00:09+00:00 2026-06-15T21:00:09+00:00

We’ve got a slight performance issue in a section of code that’s using LINQ

  • 0

We’ve got a slight performance issue in a section of code that’s using LINQ and it’s raised a question about how LINQ works in terms of lookups

My question is this (please note that I have changed all the code so this is an indicative example of the code, not the real scenario):

Given

public class Person {
 int ID;
 string Name;
 DateTime Birthday; 
 int OrganisationID;
}

If I had a list of say 100k Person objects and then a list of dates, say 1000, and I ran this code:

var personBirthdays = from Person p in personList
    where p.OrganisationID = 123
    select p.Birthday;

foreach (DateTime d in dateList)
{
    if (personBirthdays.Contains(d))
        Console.WriteLine(string.Format("Date: {0} has a Birthday", d.ToShortDateString()));
}

The resultant code would be an iteration of:

100k (the loop that needs to be done to find the users with the organisationID 123)
multiplied by
1000 (the amount of dates in the list)
multiplied by
x (the amount of users who have the organisationID 123 to be checked against for the date)

This is a lot of iterations!

If I changed the code the personBirthdays to this:

List<DateTime> personBirthdays = 
        (from Person p in personList
        where p.OrganisationID = 123
        select p.Birthday).ToList();

This should remove the 100k as a multiple by, and just do it once?

So you would have 100k + (1000 * x) instead of (100k * 1000 * x).

The question is that this seems too easy, and I’m sure the LINQ is doing something clever somewhere that should mean that this doesn’t happen.

If no one answers, I’ll run some tests and report back.

Clarity update:
We’re not considering Database lookups, the personList object is an In Memory list object. This all LINQ-to-Objects.

  • 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-15T21:00:11+00:00Added an answer on June 15, 2026 at 9:00 pm

    This should remove the 10k as a multiple by, and just do it once?

    What it means is that instead of iterating personList 100k times, performing the where and select operations for each of those iterations that you’ll be iterating the resulting List 100k times, and that the where and select operations will only have been performed on the underlying data source once.

    The question is that this seems too easy, and I’m sure the LINQ is doing something clever somewhere that should mean that this doesn’t happen.

    Nope, your first query is simply something that you shouldn’t be doing using LINQ, you should be taking the results of the query and placing them into a data structure if you plan to iterate over them many times (which is what you changed).

    You can improve this query even more by using the appropriate data structure. Searching on a List is rather inefficient, as it needs to do a linear search. It would be preferable to use a HashSet to store the results of the query. A HashSet has O(1) search speed in the average case, as opposed to O(n) search time of a List.

    var dates = new HashSet<DateTime>(from Person p in personList
                                      where p.OrganisationID = 123
                                      select p.Birthday);
    
    foreach (DateTime d in dateList.Where(date => dates.Contains(date)))
    {
        Console.WriteLine(string.Format("Date: {0} has a Birthday", d.ToShortDateString()));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am using the SimpleRSS gem to parse a WordPress RSS feed. The only

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.