-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMultiDictionaryExtensions.cs
More file actions
52 lines (31 loc) · 1.56 KB
/
MultiDictionaryExtensions.cs
File metadata and controls
52 lines (31 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System.Collections.Generic;
using System.Linq;
#if ENABLE_POLYFILLS
using Gsemac.Polyfills.System.Collections.Generic;
#endif
namespace Gsemac.Collections {
public static class MultiDictionaryExtensions {
public static void Add<TKey, TValue>(this MultiValueDictionary<TKey, TValue> dictionary, KeyValuePair<TKey, TValue> item) {
dictionary.Add(item.Key, item.Value);
}
public static bool Contains<TKey, TValue>(this MultiValueDictionary<TKey, TValue> dictionary, KeyValuePair<TKey, TValue> item) {
return dictionary.Contains(item.Key, item.Value);
}
public static void CopyTo<TKey, TValue>(this MultiValueDictionary<TKey, TValue> dictionary, KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
dictionary.Keys.SelectMany(key => dictionary[key].Select(value => new KeyValuePair<TKey, TValue>(key, value)))
.ToArray()
.CopyTo(array, arrayIndex);
}
public static bool Remove<TKey, TValue>(this MultiValueDictionary<TKey, TValue> dictionary, KeyValuePair<TKey, TValue> item) {
return dictionary.Remove(item.Key, item.Value);
}
public static bool TryGetValue<TKey, TValue>(this MultiValueDictionary<TKey, TValue> dictionary, TKey key, out TValue value) {
if (dictionary.TryGetValue(key, out IReadOnlyCollection<TValue> values) && values.Any()) {
value = values.First();
return true;
}
value = default;
return false;
}
}
}