Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if all items are the same in a List

I have a List(Of DateTime) items. How can I check if all the items are the same with a LINQ query? At any given time there could be 1, 2, 20, 50 or 100 items in the list.

like image 228
Saif Khan Avatar asked Mar 15 '11 03:03

Saif Khan


2 Answers

Like this:

if (list.Distinct().Skip(1).Any()) 

Or

if (list.Any(o => o != list[0])) 

(which is probably faster)

like image 154
SLaks Avatar answered Oct 05 '22 06:10

SLaks


I created simple extension method mainly for readability that works on any IEnumerable.

if (items.AreAllSame()) ... 

And the method implementation:

    /// <summary>     ///   Checks whether all items in the enumerable are same (Uses <see cref="object.Equals(object)" /> to check for equality)     /// </summary>     /// <typeparam name="T"></typeparam>     /// <param name="enumerable">The enumerable.</param>     /// <returns>     ///   Returns true if there is 0 or 1 item in the enumerable or if all items in the enumerable are same (equal to     ///   each other) otherwise false.     /// </returns>     public static bool AreAllSame<T>(this IEnumerable<T> enumerable)     {         if (enumerable == null) throw new ArgumentNullException(nameof(enumerable));          using (var enumerator = enumerable.GetEnumerator())         {             var toCompare = default(T);             if (enumerator.MoveNext())             {                 toCompare = enumerator.Current;             }              while (enumerator.MoveNext())             {                 if (toCompare != null && !toCompare.Equals(enumerator.Current))                 {                     return false;                 }             }         }          return true;     } 
like image 39
MSkuta Avatar answered Oct 05 '22 06:10

MSkuta