@@ -15,7 +15,7 @@ namespace CommunityToolkit.Mvvm.Collections;
15
15
/// </summary>
16
16
/// <typeparam name="TKey">The type of the group keys.</typeparam>
17
17
/// <typeparam name="TElement">The type of elements in the collection.</typeparam>
18
- public sealed class ObservableGroupedCollection < TKey , TElement > : ObservableCollection < ObservableGroup < TKey , TElement > >
18
+ public sealed class ObservableGroupedCollection < TKey , TElement > : ObservableCollection < ObservableGroup < TKey , TElement > > , ILookup < TKey , TElement >
19
19
where TKey : notnull
20
20
{
21
21
/// <summary>
@@ -30,10 +30,17 @@ public ObservableGroupedCollection()
30
30
/// </summary>
31
31
/// <param name="collection">The initial data to add in the grouped collection.</param>
32
32
public ObservableGroupedCollection ( IEnumerable < IGrouping < TKey , TElement > > collection )
33
- : base ( collection . Select ( static c => new ObservableGroup < TKey , TElement > ( c ) ) )
33
+ : base ( collection . Select ( static group => new ObservableGroup < TKey , TElement > ( group ) ) )
34
34
{
35
35
}
36
36
37
+ /// <inheritdoc/>
38
+ IEnumerable < TElement > ILookup < TKey , TElement > . this [ TKey key ]
39
+ {
40
+ // TODO: optimize this
41
+ get => Enumerable . FirstOrDefault < ObservableGroup < TKey , TElement > > ( this , item => EqualityComparer < TKey > . Default . Equals ( item . Key , key ) ) ?? Enumerable . Empty < TElement > ( ) ;
42
+ }
43
+
37
44
/// <summary>
38
45
/// Tries to get the underlying <see cref="List{T}"/> instance, if present.
39
46
/// </summary>
@@ -46,4 +53,17 @@ internal bool TryGetList([NotNullWhen(true)] out List<ObservableGroup<TKey, TEle
46
53
47
54
return list is not null ;
48
55
}
56
+
57
+ /// <inheritdoc/>
58
+ bool ILookup < TKey , TElement > . Contains ( TKey key )
59
+ {
60
+ // TODO: optimize this
61
+ return Enumerable . Any < ObservableGroup < TKey , TElement > > ( this , item => EqualityComparer < TKey > . Default . Equals ( item . Key , key ) ) ;
62
+ }
63
+
64
+ /// <inheritdoc/>
65
+ IEnumerator < IGrouping < TKey , TElement > > IEnumerable < IGrouping < TKey , TElement > > . GetEnumerator ( )
66
+ {
67
+ return GetEnumerator ( ) ;
68
+ }
49
69
}
0 commit comments