Eventuali duplicati:
Test whether two IEnumerable<T> have the same values with the same frequenciesQual è il modo più breve per confrontare se due IEnumerable <T> hanno gli stessi elementi in C#?
ho scritto
AGGIORNAMENTO - Correzione:
static bool HaveSameItems<T>(this IEnumerable<T> self, IEnumerable<T> other)
{
return !
(
other.Except(this).Any() ||
this.Except(other).Any()
);
}
non c'è un modo più breve? So che c'è SequenceEqual
ma l'ordine non ha importanza per me.
Nota che c'è un bug nel tuo codice: devi usare "Eccetto" in entrambe le direzioni, dato che in realtà vuoi controllare che [disgiunzione esclusiva] (http://en.wikipedia.org/wiki/ Exclusive_disjunction) è vuoto. –
@Wim corretto! –
Questo ha un bug. Restituisce vero per '{1, 1, 2}' e '{1, 2, 2}'. – jason