public static ObservableGroup <TKey, TValue>?FirstOrDefault <TKey, TValue>(this ObservableGroupedCollection <TKey, TValue> source, TKey key) { if (source.TryGetList(out var list)) { foreach (var group in list !) { if (EqualityComparer <TKey> .Default.Equals(group.Key, key)) { return(group); } } return(null); } return(FirstOrDefaultWithLinq(source, key)); }
/// <summary> /// Initializes a new instance of the <see cref="ReadOnlyObservableGroupedCollection{TKey, TValue}"/> class. /// </summary> /// <param name="collection">The source collection to wrap.</param> public ReadOnlyObservableGroupedCollection(ObservableGroupedCollection <TKey, TValue> collection) : this(collection.Select(static g => new ReadOnlyObservableGroup <TKey, TValue>(g)))
/// <summary> /// Initializes a new instance of the <see cref="ReadOnlyObservableGroupedCollection{TKey, TValue}"/> class. /// </summary> /// <param name="collection">The source collection to wrap.</param> public ReadOnlyObservableGroupedCollection(ObservableGroupedCollection <TKey, TValue> collection) : this(collection.Select(g => new ReadOnlyObservableGroup <TKey, TValue>(g))) { ((INotifyCollectionChanged)collection).CollectionChanged += this.OnSourceCollectionChanged; }
public static TValue ElementAt <TKey, TValue>( this ObservableGroupedCollection <TKey, TValue> source, TKey key, int index) => source.First(key)[index];
private static ObservableGroup <TKey, TValue>?FirstOrDefaultWithLinq <TKey, TValue>( ObservableGroupedCollection <TKey, TValue> source, TKey key) => source.FirstOrDefault(group => EqualityComparer <TKey> .Default.Equals(group.Key, key));
/// <summary> /// Return the first group with <paramref name="key"/> key or null if not found. /// </summary> /// <typeparam name="TKey">The type of the group key.</typeparam> /// <typeparam name="TValue">The type of the items in the collection.</typeparam> /// <param name="source">The source <see cref="ObservableGroupedCollection{TKey, TValue}"/> instance.</param> /// <param name="key">The key of the group to query.</param> /// <returns>The first group matching <paramref name="key"/> or null.</returns> public static ObservableGroup <TKey, TValue> FirstOrDefault <TKey, TValue>(this ObservableGroupedCollection <TKey, TValue> source, TKey key) => source.FirstOrDefault(group => GroupKeyPredicate(group, key));