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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:33:19+00:00 2026-05-26T06:33:19+00:00

I have a Dictionary<string, List<int>> in my code which I am using in the

  • 0

I have a Dictionary<string, List<int>> in my code which I am using in the following manner:

Key           Values  
2011-07-15    1, 2, 3
2011-07-20    4, 5, 6
2010-02-11    7, 8, 9

My code needs to be able to query for all values matching a particular substring in the key. For example, if I had the substring 2011-07 it should return values {1, 2, 3, 4, 5, 6}. A substring of 11 should return all IDs from 1-9.

Can anyone recommend a concise way to achieve this? Or provide a better data structure for retrieving this information?

  • 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-26T06:33:20+00:00Added an answer on May 26, 2026 at 6:33 am

    I would do an extension method :

    public static class DictionaryExt
    {
        public static IEnumerable<T> PartialMatch<T>(this Dictionary<string, T> dictionary, string partialKey)
        {
            // This, or use a RegEx or whatever.
            IEnumerable<string> fullMatchingKeys = 
                dictionary.Keys.Where(currentKey => currentKey.Contains(partialKey));
    
            List<T> returnedValues = new List<T>();
    
            foreach (string currentKey in fullMatchingKeys)
            {
                returnedValues.Add(dictionary[currentKey]);
            }
    
            return returnedValues;
        }
    }
    

    The “cost” of adding values to the dictionary wouldn’t change, but the cost of retrieval would be higher, but only when you know you’re going with a partial match.

    Btw, I’m sure you could transform this in a single Lambda expression, but the concept remains the same.

    Edit: In your example, this method would return 2 lists of values, but you can change it to merge the lists. Here is the extension method you could do :

    public static IEnumerable<T> PartialMatch<T>(
        this Dictionary<string, IEnumerable<T>> dictionary,
        string partialKey)
    {
        // This, or use a RegEx or whatever.
        IEnumerable<string> fullMatchingKeys = 
            dictionary.Keys.Where(currentKey => currentKey.Contains(partialKey));
    
        List<T> returnedValues = new List<T>();
    
        foreach (string currentKey in fullMatchingKeys)
        {
            returnedValues.AddRange(dictionary[currentKey]);
        }
    
        return returnedValues;
    }
    

    Edit 2: Come to think of it, you could also make it more generic. With the next extension method, it would work on any dictionary, as long as you provide a comparer that check what you mean by “partial match” :

    public static IEnumerable<TValue> PartialMatch<TKey, TValue>(
        this Dictionary<TKey, IEnumerable<TValue>> dictionary,
        TKey partialKey,
        Func<TKey, TKey, bool> comparer)
    {
        // This, or use a RegEx or whatever.
        IEnumerable<TKey> fullMatchingKeys = 
            dictionary.Keys.Where(currentKey => comparer(partialKey, currentKey));
    
        List<TValue> returnedValues = new List<TValue>();
    
        foreach (TKey currentKey in fullMatchingKeys)
        {
            returnedValues.AddRange(dictionary[currentKey]);
        }
    
        return returnedValues;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Dictionary<int, string> which I want to take the Key collection into
I have the following code which produces a dictionary containing multiple lists; each list
I have a function which returns a List<Dictionary<string, object>> where object is a standard
I have a Dictionary of dictionaries : SortedDictionary<int,SortedDictionary<string,List<string>>> I would like to merge two
I have the following code: class exampleClass { int var1; int var2; string var3
I have this code, void Generate(List<string> comb, string prefix, string remaining) { int currentDigit
I have code like the following: class MyClass { string Name; int NewInfo; }
If I have a: Dictionary<string, int> How do I copy all the values into
I have a list string tag. I am trying to initialize a dictionary with
I have a Dictionary<string, List<Order>> and I want to have the list of keys

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.