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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:50:30+00:00 2026-06-01T18:50:30+00:00

I have a process I’ve inherited that I’m converting to C# from another language.

  • 0

I have a process I’ve inherited that I’m converting to C# from another language. Numerous steps in the process loop through what can be a lot of records (100K-200K) to do calculations. As part of those processes it generally does a lookup into another list to retrieve some values. I would normally move this kind of thing into a SQL statement (and we have where we’ve been able to) but in these cases there isn’t really an easy way to do that. In some places we’ve attempted to convert the code to a stored procedure and decided it wasn’t working nearly as well as we had hoped.

Effectively, the code does this:

var match = cost.Where(r => r.ryp.StartsWith(record.form.TrimEnd()) && 
                       r.year == record.year && 
                       r.period == record.period).FirstOrDefault();

cost is a local List type. If I was doing a search on only one field I’d probably just move this into a Dictionary. The records aren’t always unique either.

Obviously, this is REALLY slow.

I ran across the open source library I4O which can build indexes, however it fails for me in various queries (and I don’t really have the time to attempt to debug the source code). It also doesn’t work with .StartsWith or .Contains (StartsWith is much more important since a lot of the original queries take advantage of the fact that doing a search for “A” would find a match in “ABC”).

Are there any other projects (open source or commercial) that do this sort of thing?

EDIT:

I did some searching based on the feedback and found Power Collections which supports dictionaries that have keys that aren’t unique.

I tested ToLookup() which worked great – it’s still not quite as fast as the original code, but it’s at least acceptable. It’s down from 45 seconds to 3-4 seconds. I’ll take a look at the Trie structure for the other look ups.

Thanks.

  • 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-01T18:50:31+00:00Added an answer on June 1, 2026 at 6:50 pm

    Looping through a list of 100K-200K items doesn’t take very long. Finding matching items within the list by using nested loops (n^2) does take long. I infer this is what you’re doing (since you have assignment to a local match variable).

    If you want to quickly match items together, use .ToLookup.

    var lookup = cost.ToLookup(r => new {r.year, r.period, form = r.ryp});
    
    foreach(var group in lookup)
    {
      // do something with items in group.
    }
    

    Your startswith criteria is troublesome for key-based matching. One way to approach that problem is to ignore it when generating keys.

    var lookup = cost.ToLookup(r => new {r.year, r.period });
    var key = new {record.year, record.period};
    string lookForThis = record.form.TrimEnd();
    var match = lookup[key].FirstOrDefault(r => r.ryp.StartsWith(lookForThis))
    

    Ideally, you would create the lookup once and reuse it for many queries. Even if you didn’t… even if you created the lookup each time, it will still be faster than n^2.

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

Sidebar

Related Questions

I have Process objects that are monitored from two different views. A Windows.Forms.ListView (actually
I have a process that interfaces with a library that launches another process. Occasionally
I have 1 process that receives incoming connection from port 1000 in 1 linux
I have a process in Linux that's getting a segmentation fault. How can I
I have a process intensive task that I would like to run in the
I have a process which iterates through a bunch of ActiveRecord models, does some
I have to process XML files that have a DTD with a XSLT in
I have a process I run periodically on a background thread that receives changes
I have a process that keeps dying in the same place, and claims to
I have to process a loop with backgroundworkers. Before I start a new loop

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.