2

Code line below prints System.Datetime

log.InfoFormat("{0}", row[2].GetType());

But row[2].GetType() is never equals to System.DateTime in condition below.

f.TestDate = (row[2].GetType() is System.DateTime) ? f.TestDate = (System.DateTime)row[2] : f.TestDate = DateTime.MinValue;

Why?

vico
  • 17,051
  • 45
  • 159
  • 315

2 Answers2

5
row[2].GetType() is System.DateTime

does not check whether row[2] is of the type System.DateTime but whether the Type instance returned by GetType() is (which it obviously isn't)

Just use

row[2] is System.DateTime
adjan
  • 13,371
  • 2
  • 31
  • 48
  • Actually, his line can be shortened to `f.TestDate = (row[2] as DateTime?) ?? DateTime.MinValue`. – ckuri Oct 07 '18 at 17:35
0

You should use:

row[2].GetType() == typeof(DateTime)

or

row[2] is DateTime
smolchanovsky
  • 1,775
  • 2
  • 15
  • 29