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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:42:21+00:00 2026-05-25T02:42:21+00:00

i have a problem with Dictionary, hope you’ll help me. I have the following

  • 0

i have a problem with Dictionary, hope you’ll help me.

I have the following declaration:

class MainCollection<TKey1, TKey2, TValue> : Dictionary<KeyValuePair<TKey1, TKey2>, TValue>

The problem is that i cant get an element from this dictionary by TKey1 OR TKey2.
Is there a way to get an element only by TKey1 OR TKey2, not TKey1 AND TKey2?

I wrote the following code:

 public TValue GetItemByKey1(TKey1 key)
 {
     MainCollection<int, int, string> Coll = new MainCollection<int, int, string>();
     var value = from s in Coll where s.Key.Key == key select s.Value;
 }

But it already has two issues:

  1. Compilation error: s.Key.Key == key => operator == can not be applied to types int and TKey1
  2. It looks ugly. Even if compilation would be successful I’m not sure that this is the fastest way to get such items. I guess that Dictionary should something better.

How can i solve such errors? I didnt find any related questions here.
Thanks 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-05-25T02:42:22+00:00Added an answer on May 25, 2026 at 2:42 am

    Okay, so you want to be able to lookup by TKey1 or TKey2. Then what you want is three dictionaries, one for each of the keys, and then one for the key-pairs.

    class Foo<TFirstKey, TSecondKey, TValue> {
        private readonly Dictionary<TFirstKey, List<TValue>> firstDictionary
            = new Dictionary<TFirstKey, List<TValue>>();
        private readonly Dictionary<TSecondKey, List<TValue>> secondDictionary
            = new Dictionary<TSecondKey, List<TValue>>();
        private Dictionary<Tuple<TFirstKey, TSecondKey>, TValue> dictionary
            = new Dictionary<Tuple<TFirstKey, TSecondKey>, TValue>();
    
        public IEnumerable<TValue> GetByFirstKey(TFirstKey firstKey) {
            return this.firstDictionary[firstKey];
        }
    
        public IEnumerable<TValue> GetBySecondKey(TSecondKey secondKey) {
            return this.secondDictionary[secondKey];
        }
    
        public TValue GetByKey(TFirstKey firstKey, TSecondKey secondKey) {
            return this.dictionary[Tuple.Create(firstKey, secondKey)];
        }
    
        public void Add(TFirstKey firstKey, TSecondKey secondKey, TValue value) {
            this.dictionary.Add(Tuple.Create(firstKey, secondKey), value);
            if(this.firstDictionary.Keys.Contains(firstKey)) {
                this.firstDictionary[firstKey].Add(value);
            }
            else {
                this.firstDictionary.Add(firstKey, new List<TValue> { value });
            }
             if(this.secondDictionary.Keys.Contains(secondKey)) {
                this.secondDictionary[secondKey].Add(value);
            }
            else {
                this.secondDictionary.Add(secondKey, new List<TValue> { value });
            }
        }
    }
    

    Note that only lookup by (TFirstKey, TSecondKey) is unique, so you need GetByFirstKey and GetBySecondKey to return collections.

    I’ll leave the rest of the details to you.

    The point is that if you want fast lookups on either key, you need two dictionaries (one for each coordinate of the key-pair). Using one can be made to work by querying the key set, but that’s slow (it’s linear to search the keys).

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

Sidebar

Related Questions

I have a simple problem that i cannot solve. I have a dictionary: aa
I have an interesting problem, which is a function that returns a Dictionary<String,HashSet<String>> .
The problem: I have Dictionary<String, String> that I need an alias to, but I
I have a problem where I need a .NET dictionary that supports multiple items
I have a problem. I want to make a dictionary that translates english words
I have a following problem. I have a class in the entity framework model
I have problem in some JavaScript that I am writing where the Switch statement
I have a problem. I am using the XMLReader class to get a NSDictionary
I have a problem in the following two code lines NSDictionary* last = [[arrHistory
I have a problem with a WPF control that I'm trying to host in

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.