Regex or wildcard in dictionary.TryGetValue
Regex or wildcard in dictionary.TryGetValue
I have a similar problem like mentioned in this Link, fetching data from Dictionary
using partial key and my key DataType
is string
.
Dictionary
DataType
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.
Dictionary
Dictionary
Dictionary
TryGetValue
Regex
.Where(kv => regex.IsMatch(kv.Key))
IEnumerable
I'm not sure this is correct but is it possible to create a
Dictionary
like this Dictionary<Regex, MyClass> myDictionary new Dictionary<Regex, MyClass>()
and fetch using regex in myDictionary.TryGetValue("some regex", out myData)
, will this have any performance impact.– Leo
2 days ago
Dictionary
Dictionary<Regex, MyClass> myDictionary new Dictionary<Regex, MyClass>()
myDictionary.TryGetValue("some regex", out myData)
Possible duplicate of Is it possible to do a partial string match on a Dictionary string key?
– Sinatr
2 days ago
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
2 days ago
TryGetValue
Dictionary
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-♦
2 days ago
GUID+GUID2
GUID3
1 Answer
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.
Tuple
Thanks @john it worked
– Leo
2 days ago
If I solved your problem, please can you accept my answer? Thanks :)
– john
yesterday
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
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 anIEnumerable
with all the matching results.– john
2 days ago