0

I have a similar problem like mentioned in this Link, fetching data from Dictionary using partial key and my key DataType is string.

This is how my dictionary looks

Key                                   Values  
GUID1+GUID2+GUID3                     1, 2, 3
GUID1+GUID2+GUID3                     4, 5, 6
GUID1+GUID2+GUID3                     7, 8, 9

But the solution provided is fetching data from Dictionary using an extension method with linq in Dictionary. I just want to extract data from Dictionary using TryGetValue passing Regex or wildcard expression.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Leo
  • 136
  • 2
  • 2
  • 11
  • 2
    Note that you'll lose any performance gains offered by a dictionary by doing this. The reason is that the dictionary first finds items which match the key's hash code, and then performs equality comparisons on each item key to find the one that is an exact match. If you use any kind of partial/wildcard approach, it's not possible, so you might as well use something like `.Where(kv => regex.IsMatch(kv.Key))` to get an `IEnumerable` with all the matching results. – ProgrammingLlama Jun 29 '18 at 07:14
  • I'm not sure this is correct but is it possible to create a `Dictionary` like this `Dictionary myDictionary new Dictionary()` and fetch using regex in `myDictionary.TryGetValue("some regex", out myData)`, will this have any performance impact. – Leo Jun 29 '18 at 07:49
  • Possible duplicate of [Is it possible to do a partial string match on a Dictionary string key?](https://stackoverflow.com/questions/7816398/is-it-possible-to-do-a-partial-string-match-on-a-dictionary-string-key) – Sinatr Jun 29 '18 at 07:51
  • I referred it, but the solution provided there is using an extension method, but I want to fetch data using `TryGetValue` method in `Dictionary` without impacting the performance. This I have mentioned in my question @Sinatr – Leo Jun 29 '18 at 08:05
  • Can you explain more about your scenario, in case there's a better way of doing what you want. If possible, include your Regex expression. – ProgrammingLlama Jun 29 '18 at 08:17
  • Key Values GUID1+GUID2+GUID3 Data1 GUID1+GUID2+GUID3 Data2 GUID1+GUID2+GUID3 Data3. I want to fetch it like `dictionary.TryGetValue(GUID1+GUID2,out myData) – Leo Jun 29 '18 at 08:32
  • 2
    Sounds like you want a dictionary of dictionaries, where the outer one has `GUID+GUID2` as a key and the inner one has `GUID3`. – Ry- Jun 29 '18 at 08:34
  • Is it possible to build a dictionary `Dictionary<(Guid, Guid), Dictionary>`? – ProgrammingLlama Jun 29 '18 at 08:35
  • Are you saying that's already your dictionary structure? If so, you should add that to your question as it's a different answer. – ProgrammingLlama Jun 29 '18 at 08:36
  • Sorry @john thats a typo and I was in middle of editing my comment, actually I can change my dictionary but will it have any performance impact if my dictionary contains 50k records and when I want to get only one record – Leo Jun 29 '18 at 08:41

1 Answers1

1

A better way of doing this would be to have a dictionary of dictionaries:

Dictionary<Tuple<Guid, Guid>, Dictionary<Guid, string>> dictionary;

And then have extension methods for the sake of simplifying the code where you use it:

public static bool TryGetValue<TKey1, TKey2, TKey3, TValue>(this Dictionary<Tuple<TKey1, TKey2>, Dictionary<TKey3, TValue>> dict, TKey1 key1, TKey2 key2, TKey3 key3, out TValue value)
{
    if (dict.TryGetValue(new Tuple<TKey1, TKey2>(key1, key2), out var subDict) && subDict.TryGetValue(key3, out value))
    {
        return true;
    }
    value = default(TValue);
    return false;
}

public static bool Add<TKey1, TKey2, TKey3, TValue>(this Dictionary<Tuple<TKey1, TKey2>, Dictionary<TKey3, TValue>> dict, TKey1 key1, TKey2 key2, TKey3 key3, TValue value)
{
var mainKey = new Tuple<TKey1, TKey2>(key1, key2);
    if (!dict.TryGetValue(mainKey, out var subDict))
    {
        subDict = new Dictionary<TKey3, TValue>();
        dict[mainKey] = subDict;
    }
subDict.Add(key3, value);
}

So when you insert into the dictionary, you use the extension method like this:

dictionary.Add(g1, g2, g3, v1);

and then to get a value:

if (dictionary.TryGetValue(g1, g2, g3, out v1))
{

}

Of course, the key of the outer dictionary is up to you. I just used Tuple to illustrate how everything can remain strongly typed.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86