Select Distinct UnSorted from IList - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Select Distinct UnSorted from IList

Demo Code


using System.Linq;
using System.Text;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections;

public class Main{
        public static List<TResult> SelectDistinctUnSorted<TObj, TResult>(this IList<TObj> list, Func<TObj, TResult> valCalculator)
        {//from www.j a  va2  s  .c o m
            var res = new List<TResult>();
            var containsCheck = new HashSet<TResult>();

            foreach (var item in list)
            {
                var result = valCalculator(item);
                if (!containsCheck.Contains(result))
                {
                    containsCheck.Add(result);
                    res.Add(result);
                }
            }

            return res;
        }
        public static bool Contains<T>(this IEnumerable<T> me, Predicate<T> condition)
        {
            foreach (var val in me)
            {
                if (condition(val)) 
                    return true;
            }
            return false;
        }
}

Related Tutorials