Harsh Vyas Harsh Vyas - 26 days ago 11
C# Question

How to compare datetime (only dd/mm/yyyy hh:mm:ss tt not whole fff section in datetime) in linq C#?

I want to compare datetime which comes from kendo filter in (DD/MM/YYYY hh:mm:ss tt) format and wanted to compare with my database using linq expression.

IEnumerable<ValidateTaskSummaryDetails> source;
source = source.Where(o => o.TaskStartDate.Value == dtStartDate);


Here
dtStartDate
comes from Kendo, the date i have got from kendo is
'10/11/2016 15:34:45'
and the date in my database is
'10/11/2016:15:34:45'
but then also i have got error like
'Enumeration yielded no results'
I think it must take the whole lot of a date with
.fff
section also but I don't get that from kendo (i.e. I don't want to get it).

The problem here is When i tried to
compare both dates
which are identical,
But in comparison system compared with
'DD/MM/YYYY hh:mm:ss.fff tt'
now the problem is with
'.fff'
due to this dates are not compared and hence
yielded no result
.

Do any have any solution?.

Answer

both DateTime are same if you remove the millisecond portion.

If so, you can truncate the milliseconds part of your TaskStartDate.Value like;

source = source.Where(o => 
                      o.TaskStartDate.Value.AddTicks(-(o.TaskStartDate.Value.Ticks % TimeSpan.TicksPerSecond)) 
                      == dtStartDate);