2009-11-15

LINQ - równość dwóch zbiorów

Poniższa metoda rozszerzona sprawdza czy oba zbiory zawierają takie same elementy:
public static bool Exact<T>(this IEnumerable<T> a_enumerable, IEnumerable<T> a_values)
{
    List<T> list = new List<T>(a_values);

    int init_count = list.Count;
    int count = 0;

    foreach (T ele in a_enumerable)
    {
        count++;

        if (count > init_count)
            return false;

        int index = list.IndexOf(ele);
        if (index == -1)
            return false;
        else
            list.RemoveAt(index);
    }

    return count == init_count;
}

Brak komentarzy:

Prześlij komentarz