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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:39:47+00:00 2026-05-11T03:39:47+00:00

When using a SortedDictionary in Linq and iterating over the KeyValuePair it provides, can

  • 0

When using a SortedDictionary in Linq and iterating over the KeyValuePair it provides, can I be assured that a complex linq query will execute it in ascending order? Here’s a brief, although a bit confusing example:

Random r = new Random(); //build 100 dictionaries and put them into a sorted dictionary //with 'priority' as the key and it is a number 0-99. SortedDictionary<int, Dictionary<int, double>> sortedDict =      new SortedDictionary<int, Dictionary<int, double>>(); for (int i = 0; i < 100; i++) {     Dictionary<int, double> dict = new Dictionary<int, double>();     //create the dictionary and a random 10 k/v pairs     for (int j = 0; j < 10; j++)     {         dict[r.Next(0, 100)] = r.NextDouble() * i * 10;     }     sortedDict[i] = dict; }  IEnumerable<int> keys = Enumerable.Range(0, 100);  //the goal is to find the FIRST existence of the 'key' inside one //of the inner dictionaries going through the SortedDictionary IN ORDER //this appears to work: var qry = from key in keys           from priority in sortedDict           where priority.Value.ContainsKey(key)           let value = priority.Value[key]           group value by key into keyGroup           let firstValue = keyGroup.First()           select new { Key = keyGroup.Key, Value = firstValue };  // the result is as expected, a list of the numbers at most 0-99 and their // value found in the dictionary with the lowest 'priority' 

The question(s):

  1. It appears to work, but can I rely on this behavior?
  2. Is this efficient, or does the group by throw it off?
  3. Does adding ‘sortedDict.Reverse()’ work properly too? (it appears to)
  4. How would PLinq handle this – and would it still be consistent?

If this isn’t guaranteed, I know how I can pull the ‘priority’ into the grouping and order by it after the fact. But I’d rather not…

  • 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. 2026-05-11T03:39:48+00:00Added an answer on May 11, 2026 at 3:39 am
    1. Yes, you can trust the order of SortedDictionary. It would be pointless otherwise 🙂
    2. It would be slightly more efficient without the ‘let’ clause. Just do:

      var qry = from key in keys           from priority in sortedDict           where priority.Value.ContainsKey(key)           let value = priority.Value[key]           group value by key into keyGroup           select new { Key = keyGroup.Key, Value = keyGroup.First() }; 

      However, you’re still scanning through the dictionary quite a lot with this. Don’t you basically want a reverse map from key to priorities containing that key? That could be constructed much more efficiently. As you say, the example is pretty confusing. If you could tell us what you’re trying to achieve in your real code, we may be able to come up with something better. (If it’s exactly this situation, I can certainly work on that – I’d rather not do so and then find out that reality is very different though!)

    3. Calling Reverse() will indeed work – but it will buffer all the data. If you want it in reverse order, I suggest you give the SortedDictionary an appropriate IComparer to start with.

    4. Parallel LINQ is ‘interesting’ when it comes to order. It may be different right now, but I had fun a while ago when I was plotting the Mandelbrot set using it…

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

Sidebar

Ask A Question

Stats

  • Questions 82k
  • Answers 82k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I tend to put utilities into one (or a few)… May 11, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer From the demo: http://www.trirand.com/jqgrid35/jqgrid.html (under new in version 3.3->tree grid)… May 11, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer You'll always get mouse co-ordinates for a submit button type="image"… May 11, 2026 at 4:36 pm

Related Questions

I have a large collection of strings (up to 1M) alphabetically sorted. I have
I recently started working with Silverlight and immediately noticed the difference between the Silverlight
When using a DataReader object to access data from a database (such as SQL
When using a browser to transform XML (Google Chrome or IE7) is it possible

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.