@@ -15,7 +15,7 @@ namespace CommunityToolkit.Mvvm.Collections;
1515/// </summary>
1616/// <typeparam name="TKey">The type of the group keys.</typeparam>
1717/// <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 >
1919 where TKey : notnull
2020{
2121 /// <summary>
@@ -30,10 +30,17 @@ public ObservableGroupedCollection()
3030 /// </summary>
3131 /// <param name="collection">The initial data to add in the grouped collection.</param>
3232 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 ) ) )
3434 {
3535 }
3636
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+
3744 /// <summary>
3845 /// Tries to get the underlying <see cref="List{T}"/> instance, if present.
3946 /// </summary>
@@ -46,4 +53,17 @@ internal bool TryGetList([NotNullWhen(true)] out List<ObservableGroup<TKey, TEle
4653
4754 return list is not null ;
4855 }
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+ }
4969}
0 commit comments