Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would do an extension method :</p> <pre><code>public static class DictionaryExt { public static IEnumerable&lt;T&gt; PartialMatch&lt;T&gt;(this Dictionary&lt;string, T&gt; dictionary, string partialKey) { // This, or use a RegEx or whatever. IEnumerable&lt;string&gt; fullMatchingKeys = dictionary.Keys.Where(currentKey =&gt; currentKey.Contains(partialKey)); List&lt;T&gt; returnedValues = new List&lt;T&gt;(); foreach (string currentKey in fullMatchingKeys) { returnedValues.Add(dictionary[currentKey]); } return returnedValues; } } </code></pre> <p>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.</p> <p>Btw, I'm sure you could transform this in a single Lambda expression, but the concept remains the same.</p> <p><strong>Edit</strong>: 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 :</p> <pre><code>public static IEnumerable&lt;T&gt; PartialMatch&lt;T&gt;( this Dictionary&lt;string, IEnumerable&lt;T&gt;&gt; dictionary, string partialKey) { // This, or use a RegEx or whatever. IEnumerable&lt;string&gt; fullMatchingKeys = dictionary.Keys.Where(currentKey =&gt; currentKey.Contains(partialKey)); List&lt;T&gt; returnedValues = new List&lt;T&gt;(); foreach (string currentKey in fullMatchingKeys) { returnedValues.AddRange(dictionary[currentKey]); } return returnedValues; } </code></pre> <p><strong>Edit 2</strong>: 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 <code>comparer</code> that check what you mean by "partial match" :</p> <pre><code>public static IEnumerable&lt;TValue&gt; PartialMatch&lt;TKey, TValue&gt;( this Dictionary&lt;TKey, IEnumerable&lt;TValue&gt;&gt; dictionary, TKey partialKey, Func&lt;TKey, TKey, bool&gt; comparer) { // This, or use a RegEx or whatever. IEnumerable&lt;TKey&gt; fullMatchingKeys = dictionary.Keys.Where(currentKey =&gt; comparer(partialKey, currentKey)); List&lt;TValue&gt; returnedValues = new List&lt;TValue&gt;(); foreach (TKey currentKey in fullMatchingKeys) { returnedValues.AddRange(dictionary[currentKey]); } return returnedValues; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload