Equal between two IEnumerable - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IEnumerable

Description

Equal between two IEnumerable

Demo Code


using System;//from  ww w.ja v a2 s  .c o m
using System.Linq;
using System.Collections.Generic;

public class Main{
    public static bool Eq<T> (IEnumerable<T> c1, IEnumerable<T> c2, Comparer<T> comparer)
      {
         if (c1 == null && c2 == null)
            return true;

         if (c1 != null && c2 == null)
            return false;

         if (c1 == null && c2 != null)
            return false;

         var a1 = c1.ToList ();
         var a2 = c2.ToList ();

         if (a1.Count != a2.Count)
            return false;

         var difference = a1.Minus (a2, comparer).ToList ();
         return !difference.Any ();
      }
    public static IEnumerable<T> Minus<T> (this IEnumerable<T> source, IEnumerable<T> what, Comparer<T> comparer, Cloner<T> cloner)
      {
         var v = what.ToList ();
         foreach (var t in source)
            if (v.All (i => !comparer (t, i)))
               yield return cloner (t);
      }
    public static IEnumerable<T> Minus<T> (this IEnumerable<T> source, IEnumerable<T> what, Comparer<T> comparer)
      {
         var v = what.ToList ();
         foreach (var t in source)
            if (v.All (i => !comparer (t, i)))
               yield return t;
      }
}

Related Tutorials