samples/TodoApp/TodoApp.Data/Extensions/CollectionExtensions.cs
/// <summary>
/// Adds an item within a collection only if there are no items identified by the match function
/// </summary>
/// <typeparam name="T">The type of the collection model.</typeparam>
/// <param name="collection">The collection.</param>
/// <param name="match">The match function</param>
/// <param name="item">The model item to add.</param>
Adds an item within a collection only if there are no items identified by the match function
Adds an item within a collection only if there are no items identified by the match function
public static bool AddIfMissing<T>(this Collection<T> collection, Func<T, bool> match, T item)
        {
            var itemsInCollection = collection.Where(match).ToArray();
            if (itemsInCollection.Length == 0)
            {
                collection.Add(item);
                return true;
            }
            return false;
        }
public static bool AddIfMissing < T > ( this Collection < T > collection , Func < T , bool > match , T item ) { var itemsInCollection = collection . Where ( match ) . ToArray ( ) ; if ( itemsInCollection . Length == 0 ) { collection . Add ( item ) ; return true ; } return false ; }
Adds an item within a collection only if there are no items identified by the match function
Adds an item within a collection only if there are no items identified by the match function
Collection<T>
this
match
Func<T, bool>
The match function
The match function
item
T
The model item to add.
The model item to add .
collection
None
typeparam
The type of the collection model.
The type of the collection model .