Check if all items are the same in a List

后端 未结 5 1451
独厮守ぢ
独厮守ぢ 2020-12-02 22:11

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.

5条回答
  •  无人及你
    2020-12-02 22:29

    Like this:

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

    Or

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

    (which is probably faster)

提交回复
热议问题