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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:34:00+00:00 2026-05-31T12:34:00+00:00

Hopefully the comments explain the question. I have a large static (gets built with

  • 0

Hopefully the comments explain the question. I have a large static (gets built with fresh data on program load) dictionary that is used a lot and I want to reference it as efficiently as possible rather than create copies of the data and eat memory. Have a hashset the represents a subset of that dictionary that I need to represent as a sorted list of values. New HashSet or changes to the HashSet then I build a new class that wraps the code below. Is there a better way to go about this? I could not figure out how to use an external HashSet in LINQ.

// FTSwordIDs is a hashset and is also used elsewhere - a lot - hashset for lookup speed
// dlFTSword is Dictionary<Int32, string> and is static and sorted by value (on load) 
// dlFTSword can contain over a million entries and is used a lot of places 
// need to refence it rather than build a new list and eat memory
words = new List<string>();
foreach(Int32 id in dlFTSword.Keys)
{
    if (FTSwordIDs.Contains(id)) words.Add(dlFTSword[id]);
}
return words;

The time is 12 ms

try
{
    sqlConRO1.Open();
    sqlCMDRO1.CommandText = "SELECT [ID], [word] FROM [FTSwordDef] WITH (NOLOCK); "; // ORDER BY [word]; ";
    SqlDataReader rdr = sqlCMDRO1.ExecuteReader();
    while (rdr.Read())
    {
        dlFTSword.Add(rdr.GetInt32(0), rdr.GetString(1));
    }
    rdr.Close();
    Debug.WriteLine("dlFTSword.Count = " + dlFTSword.Count.ToString());

}
catch (Exception Ex) { throw new Exception("InitializeData Failed " + Ex.Message); }
finally { sqlConRO1.Close(); }

HashSet<Int32> wordIDs = new HashSet<int>() { 1, 100000, 200000, 300000, 400000, 500000, 600000 };
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();

//List<string> words = dlFTSword.Where(pair => wordIDs.Contains(pair.Key))
//         .Select(pair => pair.Value)
//         .OrderBy(x => x)
//         .ToList();
//List<string> words = wordIDs.Where(key => dlFTSword.ContainsKey(key))
//          .Select(key => dlFTSword[key])
//          .OrderBy(value => value)
//          .ToList();
IEnumerable<string> words = wordIDs.Where(key => dlFTSword.ContainsKey(key))
          .Select(key => dlFTSword[key])
          .OrderBy(value => value);
DateTime dtEnd = DateTime.Now;
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Debug.WriteLine(ts.Milliseconds.ToString());
  • 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-31T12:34:01+00:00Added an answer on May 31, 2026 at 12:34 pm

    Sounds like you want:

    var words = dlFTSword.Where(pair => FTSwordIDs.Contains(pair.Key))
                         .Select(pair => pair.Value)
                         .ToList();
    

    Note that this avoids performing a lookup on each key after finding that it’s present. We don’t actually look anything up in the dictionary. It also doesn’t involve creating any other sets, which Intersect would.

    This is worth checking though:

    dlFTSword is Dictionary and is static and sorted by value (on load)

    If it’s a Dictionary<TKey, TValue> then it isn’t sorted. There’s no such concept as a “sorted” Dictionary<TKey, TValue>. You could use a SortedDictionary<,> or SortedList<,>, but those are both different to a Dictionary<,>.

    EDIT: If the hash set is really small, it makes more sense to iterate over that than over every pair in the dictionary:

    var words = FTSwordIDs.Where(key => dlFTSword.ContainsKey(key))
                          .Select(key => dlFTSword[key])
                          .OrderBy(value => value)
                          .ToList();
    

    It’s slightly ugly to have to do the lookup, but it’ll be significantly faster.

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

Sidebar

Related Questions

Hopefully I can explain this clearly enough. I have a table that displays some
In my ruby-on-rails app, I have nested comments that can be nested an arbitrary
I have asked this question before and it seemed that the code I was
Hopefully this will not spark a religious war... We have a web based app
Hopefully (but not necessarily) one that is independent of language or framework?
Hopefully an easy question, but I'd quite like a technical answer to this! What's
Let me explain my problem, and hopefully someone can offer some good advice. I
I have been writing some code that creates a generic blog. Its features are
I also have a very large table in SQL Server (2008 R2 Developer Edition)
UPDATE: It was suggested in the comments that I create a wiki for this.

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.