Check if all items are the same in a List

Saif Khan picture Saif Khan · Mar 15, 2011 · Viewed 63.3k times · Source

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.

Thanks

Answer

SLaks picture SLaks · Mar 15, 2011

Like this:

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

Or

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

(which is probably faster)