Why my IEnumerable dates does not contain my date

I have a list of Brazilian holidays, so I got this:

[0] {01/01/2010 00:00:00}   System.DateTime
[1] {16/02/2010 00:00:00}   System.DateTime
[2] {02/04/2010 00:00:00}   System.DateTime
[3] {04/04/2010 00:00:00}   System.DateTime
[4] {21/04/2010 00:00:00}   System.DateTime
[5] {01/05/2010 00:00:00}   System.DateTime

[6] {03/06/2010 00:00:00}   System.DateTime

[7] {07/09/2010 00:00:00}   System.DateTime
[8] {12/10/2010 00:00:00}   System.DateTime
[9] {02/11/2010 00:00:00}   System.DateTime
[10]{15/11/2010 00:00:00}   System.DateTime
[11]{25/12/2010 00:00:00}   System.DateTime

And when in the method I get dateTime = {03/06/2010 19:00:00}and call:

HolidayDates.Contains(dateTime)

Why does it return false? What is the best way to handle this? Publication date?

+3
source share
2 answers

It returns false because your dateTime value has a time component, so this is a different time.

To disable the call component time

HolidayDates.Contains(dateTime.Date)

If you always store dates in your IEnumerable from 00:00:00 as a time component, then this will work.

+14
source

The collection does not have this value. There is a matching date, but your time is 19:00, while the time in the collection is 00:00.

, :

HolidayDates.Select(d => d.Date).Contains(dateTime.Date)

, . .

+2

Source: https://habr.com/ru/post/1760375/


All Articles