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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:55:59+00:00 2026-05-25T01:55:59+00:00

I have two Dictionary<string, Item> s. Item has a has a public property int

  • 0

I have two Dictionary<string, Item>s. Item has a has a public property int Level.

I want to combine these two dictionaries where keys are unique and I want to be able to specify level on both.

Something like

dictionary 1 = all items that level < 10
dictionary 2 = all items level < 20

combine dictionary 2 with 1 (where Value.Level < 10)
if the Key is unique and the Value.Level < 20

I can easily do this with foreach loops. I can also do this with multiple linq queries.
However i can’t seem to figure out how to make this one single linq query.

Edit- Per your request John here is the code with foreach

Dictionary<string, Dictionary<string, Item>> itemDictionary = new Dictionary<string, Dictionary<string, Item>>();

Dictionary<string, Item> items = new Dictionary<string,Item>();

            if (itemDictionary.ContainsKey(comboBox.Text))
            {
                foreach (KeyValuePair<string, Item> kvp in itemDictionary[comboBox.Text])
                {
                    if (!items.ContainsKey(kvp.Key) && kvp.Value.Level <= int.Parse(textBox.Text))
                        items.Add(kvp.Key, kvp.Value);
                }
            }

            if (itemDictionary.ContainsKey(comboBox1.Text))
            {
                foreach (KeyValuePair<string, Spell> kvp in itemDictionary[comboBox1.Text])
                {
                    if (!items.ContainsKey(kvp.Key) && kvp.Value.Level <= int.Parse(textBox1.Text))
                        items.Add(kvp.Key, kvp.Value);
                }
            }
            var query = from s in items
                        orderby s.Value.Level
                        select s;

            foreach (var i in query)
               listBox.Items.Add(string.Format("{0} {1}", i.Value.Level, i.Key));
  • 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-25T01:55:59+00:00Added an answer on May 25, 2026 at 1:55 am

    Ok, that code makes it clear what you want to accomplish. So the end result should be a dictionary comprised of the items from both dictionaries that meet the specified level. If an item exists in both dictionaries than the item from the first dictionary will be preferred. While it is possible to accomplish this in a single Linq query you would end up repeating some work. Here is what I came up with, it runs in LinqPad if you want to try it out easily.

    var itemsOne = new[] {
        new { Name = "A", Level = 1 },
        new { Name = "B", Level = 2 },
        new { Name = "C", Level = 3 },
        new { Name = "D", Level = 4 }
    }.ToDictionary(i => i.Name, i => i);
    
    var itemsTwo = new[] {
        new { Name = "C", Level = 10 },
        new { Name = "D", Level = 20 },
        new { Name = "E", Level = 30 },
        new { Name = "F", Level = 40 }
    }.ToDictionary(i => i.Name, i => i);
    
    var itemsOneLevel = 3;
    var itemsTwoLevel = 30;
    
    var validFromItemsOne = (from item in itemsOne
                             where item.Value.Level <= itemsOneLevel
                             select item).ToDictionary(i => i.Key, i => i.Value);
    
    var validFromItemsTwo = from item in itemsTwo
                            where item.Value.Level <= itemsTwoLevel
                                && !validFromItemsOne.ContainsKey(item.Key)
                            select item;
    
    var items = validFromItemsOne
        .Concat(validFromItemsTwo)
        .ToDictionary(i => i.Key, i => i.Value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two dictionaries with the same structure: Dictionary<string, int> foo = new Dictionary<string,
I have a Dictionary of dictionaries : SortedDictionary<int,SortedDictionary<string,List<string>>> I would like to merge two
I have two complex dictionaries in the form Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>> So as
I have two dictionaries with a string key and different value types. private Dictionary<string,
Here is my problem: I have two dictionaries with identical structures: Dictionary<string, List<Object>> Existing
I have two dictionaries like Dictionary<String, String> one = new Dictionary<string, string> { {
I have two string, that i d like to use them as the dictionary
I have a model public class Foo{ public int Id{get;set;} public string Name {get;
I have two Dictionary<string, byte> properties in my model that should validate properly with
Hi guys I have a dictionary which has to be shared between two different

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.