Finds all the elements that match the predicate and then applies the functor - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Finds all the elements that match the predicate and then applies the functor

Demo Code


using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System;/* w w w .  j  a  va  2  s  .com*/

public class Main{
        /// <summary>
        /// Finds all the elements that match the predicate and then applies the functor
        /// </summary>
        /// <typeparam name="T">Type of the collection</typeparam>
        /// <typeparam name="TResult">Type of the result</typeparam>
        /// <param name="collection">Collection to use</param>
        /// <param name="predicate">Predicate to filter</param>
        /// <param name="functor">Functor to use</param>
        /// <returns>The collection of elements that match the predicate with the functor applied</returns>
        public static IEnumerable<TResult> FindAll<T, TResult>(this IEnumerable collection, Predicate<T> predicate, Func<T, TResult> functor)
        {
            return collection.Cast<T>().FindAll(predicate, functor);
        }
        /// <summary>
        /// Finds all the elements that match the predicate and then applies the functor
        /// </summary>
        /// <typeparam name="T">Type of the collection</typeparam>
        /// <typeparam name="TResult">Type of the result</typeparam>
        /// <param name="collection">Collection to use</param>
        /// <param name="predicate">Predicate to filter</param>
        /// <param name="functor">Functor to use</param>
        /// <returns>The collection of elements that match the predicate with the functor applied</returns>
        public static IEnumerable<TResult> FindAll<T, TResult>(this IEnumerable<T> collection, Predicate<T> predicate, Func<T, TResult> functor)
        {
            return collection.FindAll(predicate).Select(functor).ToList();
        }
        /// <summary>
        /// Finds all the elements in the collection that satisfy the predicate
        /// </summary>
        /// <typeparam name="T">Type of the predicate</typeparam>
        /// <param name="collection">Collection to use</param>
        /// <param name="predicate">Predicate to satisfy</param>
        /// <returns>A collection with all the elements that satisfy the predicate</returns>
        public static IEnumerable<T> FindAll<T>(this IEnumerable collection, Predicate<T> predicate)
        {
            return collection.Cast<T>().FindAll(predicate);
        }
        /// <summary>
        /// Finds all the elements in the collection that satisfy the predicate
        /// </summary>
        /// <typeparam name="T">Type of the collection</typeparam>
        /// <param name="collection">Collection to search</param>
        /// <param name="predicate">Predicate to satisfy</param>
        /// <returns>A collection with all the elements that satisfy the predicate</returns>
        public static IEnumerable<T> FindAll<T>(this IEnumerable<T> collection, Predicate<T> predicate)
        {
            return collection.Where(t => predicate(t)); 
        }
}

Related Tutorials