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
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
I created simple extension method mainly for readability that works on any IEnumerable.
And the method implementation:
VB.NET version:
Or
This is an option, too:
It is faster than
if (list.Distinct().Skip(1).Any())
, and performs similarly asif (list.Any(o => o != list[0]))
, however, the difference is not significant, so I suggest using the more readable one.Like this:
Or
(which is probably faster)