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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:58:20+00:00 2026-05-21T20:58:20+00:00

The context is that I have one cached set of values in memory that

  • 0

The context is that I have one cached set of values in memory that were expensive to fetch, and another set of associated data that is inexpensive to fetch and can’t be cached (business rule). I’ve got it all working but I was just wondering if anyone could think of a less expensive way of doing this sort of update…

    foreach (var nonCachedItem in searchItemsNonCached)
    {
        foreach (var cachedItem in searchItemsCached)
        {
            if (cachedItem.ID == nonCachedItem.ID)
                nonCachedItem.Description = cachedItem.Description;
        }
    }

it’s basically just to match up the cached information with the information I just got. It all works and the load is almost negligable but I’m kind of a sucker for efficiency.

EDIT: in the above, searchItemsNonCached and searchItemsCached are both Lists of SearchItem where Searchitem is a bespoke object.

  • 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-21T20:58:21+00:00Added an answer on May 21, 2026 at 8:58 pm

    Load a Dictionary with the cached items and then cycle through each non-cached item looking for a match in the dictionary. This is O(n) as opposed to the nested loop which is O(n^2).

    var cachedDictionary = new Dictionary<int, YourType>();
    foreach (var item in searchItemsCached)
    {
      cachedDictionary.Add(item.ID, item);
    }
    foreach (var item in searchItemsNonCached)
    {
      YourType match;
      if (cachedDictionary.TryGetValue(out match))
      {
        item.Description = match.Description;
      }
    }
    

    If you could consider using a Dictionary for the cached items from the beginning (instead of a List) then you could avoid the step of loading it before finding matches.

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

Sidebar

Related Questions

I have one table that saves comments for a varied set of content types.
I have several buttons that set some variables for another class (via extern). Thing
We have two web applications and each one is creating it's own data context
I have one site that is displaying html content that needs to be displayed
Context: I have a WPF App that uses certain unmanaged DLLs in the D:\WordAutomation\MyApp_Source\Executables\MyApp
We have developed a webservice that sits and runs in the context of a
I have a winforms application that presently ships with a chm file for context-sensitive
I have a form that tries to modify a JComponent's graphics context. I use,
The situation is this: You have a Hibernate context with an object graph that
I have a SQLite database that contains a huge set of log messages. I

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.