Linq-to-SQL仅搜索日期的DATE部分 [英] Linq-to-SQL to search only DATE portions of a date
问题描述
我需要根据日期范围获取数据,例如:
SELECT * FROM MyTable WHERE
myDate> ='May 17 2012'AND
myDate< ='May 17 2012'
I在LINQ中写这个查询,问题是LINQ正在将这个查询转换为:
SELECT * FROM MyTable WHERE
/ pre>
myDate> ='May 17 2012 12:00:00:000AM'AND
myDate< ='May 17 2012 12:00:00:000AM'
问题是,LINQ也在考虑时间部分,但我只需要搜索日期部分。
有人可以帮助我吗?
感谢。
解决方案只要想和大家分享我发现的解决方案!
EntityFunctions提供了TruncateTime(),可以用于这个目的。
EntityFunctions.TruncateTime
e在MyContext.MyTable.Where(a =>
EntityFunctions.TruncateTime(a.DateTimeColumn)==
EntityFunctions.TruncateTime(DateTime.Now))select e;
EDIT:
对于EntityFramework 6及以上: p>
来自e在MyContext.MyTable.Where中(a =>
DbFunctions.TruncateTime(a.DateTimeColumn)==
DbFunctions.TruncateTime(DateTime.Now))select e;
我发现它此处。
I need to get data based on date range like:
SELECT * FROM MyTable WHERE myDate >= 'May 17 2012' AND myDate <= 'May 17 2012'
I have written this query in LINQ , the issue is that LINQ is converting this query to:
SELECT * FROM MyTable WHERE myDate >= 'May 17 2012 12:00:00:000AM' AND myDate <= 'May 17 2012 12:00:00:000AM'
The problem is that LINQ is also considering the time part but I need to search only on date part.
Can anyone help me?
Thanks.
解决方案Just wanted to share with you that i found the solution!
The EntityFunctions provide us TruncateTime(), which can used for this purpose.
EntityFunctions.TruncateTime
from e in MyContext.MyTable.Where(a => EntityFunctions.TruncateTime(a.DateTimeColumn) == EntityFunctions.TruncateTime(DateTime.Now)) select e;
EDIT: For EntityFramework 6 and above:
from e in MyContext.MyTable.Where(a => DbFunctions.TruncateTime(a.DateTimeColumn) == DbFunctions.TruncateTime(DateTime.Now)) select e;
I found it here.
这篇关于Linq-to-SQL仅搜索日期的DATE部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!