在linq C#中如何比较datetime(只有dd / mm / yyyy hh:mm:ss tt不是整个fff部分在datetime) [英] How to compare datetime (only dd/mm/yyyy hh:mm:ss tt not whole fff section in datetime) in linq C#?
问题描述
我想比较来自kendo filter(DD / MM / YYYY hh:mm:ss tt)格式的datetime,并希望使用linq表达式与我的数据库进行比较。
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);
这里 dtStartDate
来自Kendo,日期我从kendo得到的是 '10 / 11/2016 15:34:45'
,数据库中的日期是'10/11/2016 :15:34:45'
但是我也有错误,如'枚举没有结果'
我认为它必须占用很多一个日期与 .fff
部分,但我不知道从kendo(即我不想得到它)。
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).
这里的问题是当我尝试比较两个日期
这是相同的,
但是在比较系统与'DD / MM / YYYY hh:mm:ss.fff tt'
相比,现在的问题是'。fff'
由于这个日期不被比较,因此没有产生结果
。
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?.
推荐答案
如果删除毫秒部分,两个DateTime都相同。
both DateTime are same if you remove the millisecond portion.
如果是这样,你可以截断你的 TaskStartDate.Value
的毫秒部分,像;
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);
这篇关于在linq C#中如何比较datetime(只有dd / mm / yyyy hh:mm:ss tt不是整个fff部分在datetime)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!