摘要:List
擴充方法:
public delegate bool PredicateTwoParams<T>(T source, T target);
public static class ListExtension
{
public static List<T>FindNotMatch<T>(this List<T> source, List<T> target, PredicateTwoParams<T> match)
{
List<T> result = source.FindAll(item1 => !target.Exists(item2 => match.Invoke(item1, item2)));
return result;
}
}
如果也要比對不同型別的集合
public delegate bool PredicateTwoParams<TSource, TTarget>(TSource source, TTarget target);
public static class ListExtension
{
public static List<TSource> FindNotMatch<TSource, TTarget>(this List<TSource> source, List<TTarget> target, PredicateTwoParams<TSource, TTarget> match)
{
List<TSource> result = source.FindAll(item1 => !target.Exists(item2 => match.Invoke(item1, item2)));
return result;
}
}
Example:
List< int > list1 = new List<int >();
List< int > list2 = new List<int>();
list1.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
list2.AddRange(new int[] { 2, 4, 6, 8 });
List<int> result = list1.FindNotMatch(list2, (item1, item2) => item1 == item2);
result會找出1 3 5 7 9 10,共6個項目。