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

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

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

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

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

Are there any solutions?

+1
source share
2 answers

both DateTimes are the same if you delete the millisecond part.

If so, you can trim the millisecond portion of your TaskStartDate.Value like;

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

When you do an equality comparison, you're right when comparing the full DateTime representation, to be honest, it compares the equality of the Ticks property of each DateTime object.

 public static bool operator ==(DateTime d1, DateTime d2) { return d1.InternalTicks == d2.InternalTicks; } 

(from reference source )

If you only need the equality Date (year, month, day) and Time (hours, minute, seconds), you need to implement something like the following:

 IEnumerable<ValidateTaskSummaryDetails> source; source = source.Where(o => o.TaskStartDate.Value.Year == dtStartDate.Year && o.TaskStartDate.Value.Month == dtStartDate.Month && o.TaskStartDate.Value.Day == dtStartDate.Day && o.TaskStartDate.Value.Hour == dtStartDate.Hour && o.TaskStartDate.Value.Minute == dtStartDate.Minute && o.TaskStartDate.Value.Second == dtStartDate.Second); 

Or a comparison of string representation equality in the required format.

 var format = "dd/MM/yyyy HH:mm:ss"; source = source.Where(o => o.TaskStartDate.Value.ToString(format) == dtStartDate.ToString(format)); 

The first approach will be faster because it requires comparing numeric fields instead of string . It should be noted (as noted in the comments) that converting a DateTime to a string and comparing this string representation is kind of a bad idea, and this is a good example of bad smell code. It will give the desired results, but you should avoid it - string comparisons are always worse than numeric comparisons.

It is recommended to avoid comparing comparisons of DateTime objects, since there are too many variables that affect the result (Hour, Minute, Second, Day, Month, Year, Millisecond) and only comparisons of inequalities are performed (<, <=,>,> =). Or compare only the Date DateTime component as follows:

 source = source.Where(o => o.TaskStartDate.Value.Date == dtStartDate.Date); 

Often for most common applications this is enough.

+1
source

All Articles