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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:51:56+00:00 2026-06-17T01:51:56+00:00

I need help building a Linq query. I have this dictionary : var dict

  • 0

I need help building a Linq query. I have this dictionary :

var dict = new Dictionary<string, IDictionary<int, double>>  
{  
    { "one", new Dictionary<int, double>  
        {  
            { 1, 2.0 },  
            { 2, 3.0 }  
        }},  
    { "two", new Dictionary<int, double>  
        {  
            { 1, 3.0 },  
            { 2, 4.0 },  
            { 3, 5.0 }  
        }},  
    { "three", new Dictionary<int, double>  
        {  
            { 1, 4.0 },  
            { 2, 5.0}  
        }}  
    };  

I want to select all “string”/”int” tuples whose associated value is 3.0. With Foreach loops, it looks like :

var res = new Dictionary<string, int>();
foreach (var elem in dict.Select (d => new { S = d.Key, I = d.Value }))
{
    foreach (var val in elem.I)
    {
        if (val.Value == 3.0)
        {
            res.Add(elem.S, val.Key);
        }
    }
}

I’m trying to do the same with a single Linq query but with no success (I don’t know how to “join” the key with the value from a subquery). How would you do this?

Thank you in advance!

  • 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-17T01:51:57+00:00Added an answer on June 17, 2026 at 1:51 am

    You could do it this way:

    var result = dict
        // flatten to 3-value tuple
        .SelectMany(kvp => kvp.Value
            .Select(kvp2 => new // anonymous object
            {
                OuterKey = kvp.Key, // e.g. "one"
                InnerKey = kvp2.Key, // e.g. 1
                InnerValue = kvp2.Value // e.g. 2.0
            })
        ).Where(o => o.InnerValue == 3.0) // limit to the 3.0 value
        .ToDictionary(o => o.OuterKey, o => o.InnerKey)
        ;
    

    The SelectMany flattens the dictionary of dictionaries into a structure that looks like this:

    { OuterKey = "one", InnerKey = 1, InnerValue = 2.0 },
    { OuterKey = "one", InnerKey = 2, InnerValue = 3.0 },
    { OuterKey = "two", InnerKey = 1, InnerValue = 3.0 },
    { OuterKey = "two", InnerKey = 2, InnerValue = 4.0 },
    { OuterKey = "two", InnerKey = 3, InnerValue = 5.0 },
    { OuterKey = "three", InnerKey = 1, InnerValue = 4.0 },
    { OuterKey = "three", InnerKey = 2, InnerValue = 5.0 }
    

    The Where() limits it down to just the objects with InnerValue = 3.0:

    { OuterKey = "one", InnerKey = 2, InnerValue = 3.0 },
    { OuterKey = "two", InnerKey = 1, InnerValue = 3.0 }
    

    The ToDictionary() looks like this:

    { "one", 2 },
    { "two", 1 }
    

    If it’s possible for there to be more than a single 3.0 under the same outer key, then you can use ToLookup() instead of ToDictionary().

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

Sidebar

Related Questions

I need some help building an Query. I have a table Orders with 3
I need help with building SQL query. I have 4 tables: Sellers, Goods, Projects
I need help building a mysql query to select from multiple tables. I have
i need serious help here! i have this site am building: a summary of
I need some help in building the correct query. I have an Employees table.
Need a help in building query. I have quantity_price table, listing quantity and corresponding
I Really need help in this ... I'm building an application where I need
Need help with a query that I wrote: I have three tables Company id
need help/guide for sql select query, I have 2 table stock and stock_history, in
need help building a query to replace fields on a table that match a

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.