如何从日期时间在EF只比较日期组件? [英] How to compare only date components from DateTime in EF?
问题描述
我有两个日期值,一个已经存储在数据库中,并通过使用的DatePicker用户选择的其他。用例是搜索从数据库特定日期
在数据库pviously输入的值$ P $始终为12:00:00,其中从选择器输入的日期有不同的时间部分时间部分。
我感兴趣的只有日期组件和想忽略时间部分。
什么是做在C#这种比较方式?
此外,如何做到这一点的LINQ?
更新:
在LINQ到实体,以下的罚款。
E => DateTime.Compare(e.FirstDate.Value,SecondDate)> = 0
注意:是在写这个答案时,EF-关系不明确的时间(被编辑到问题后,这是书面)。对于EF正确的方法,检查 Mandeeps答案。
您可以使用 DateTime.Date
属性来执行日期只比较。
DateTime的一个= GetFirstDate();
日期时间B = GetSecondDate();如果(a.Date.Equals(b.Date))
{
//日期是平等的
}
I am having two date values, one already stored in the database and the other selected by the user using DatePicker. The use case is to search for a particular date from the database.
The value previously entered in the database always has time component of 12:00:00, where as the date entered from picker has different time component.
I am interested in only the date components and would like to ignore the time component.
What are the ways to do this comparison in C#?
Also, how to do this in LINQ?
UPDATE: On LINQ to Entities, the following works fine.
e => DateTime.Compare(e.FirstDate.Value, SecondDate) >= 0
NOTE: at the time of writing this answer, the EF-relation was unclear (that was edited into the question after this was written). For correct approach with EF, check Mandeeps answer.
You can use the DateTime.Date
property to perform a date-only comparison.
DateTime a = GetFirstDate();
DateTime b = GetSecondDate();
if (a.Date.Equals(b.Date))
{
// the dates are equal
}
这篇关于如何从日期时间在EF只比较日期组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!