C# 在linq中比较日期
我想查询数据库中日期大于或等于给定日期的项目 数据库中的日期是日期时间并记录时间 如果用户输入搜索字符串“1/30/2014”,他们希望返回在该日期任何时间出现的条目。但是,任何在上午12点之后的时间都不会返回 我知道我可以简单地在搜索字符串中添加一天,但是有更合适的方法吗C# 在linq中比较日期,c#,html,forms,linq,linq-to-entities,C#,Html,Forms,Linq,Linq To Entities,我想查询数据库中日期大于或等于给定日期的项目 数据库中的日期是日期时间并记录时间 如果用户输入搜索字符串“1/30/2014”,他们希望返回在该日期任何时间出现的条目。但是,任何在上午12点之后的时间都不会返回 我知道我可以简单地在搜索字符串中添加一天,但是有更合适的方法吗 if (form["closedend"] != "") { DateTime d = DateTime.Parse(form["closedend"]); traces = traces.Where(s
if (form["closedend"] != "")
{
DateTime d = DateTime.Parse(form["closedend"]);
traces = traces.Where(s => s.date_Closed >= d);
}
您可以使用截断时间部分:
traces = traces.Where(s => s.date_Closed.Date <= d.Date);
你说
日期大于或等于给定日期的
但是在你的代码中你写
s.date_Closed <= d
s.date\u Closed对于本例,您不需要任何其他必要的dll。您可以实现其他函数,这些函数返回bool
,例如:
public bool MyFunction(DateTime s)
{
DateTime d = DateTime.Parse(Your constructor);
return (s.date_Closed >= d);
}
var query = from obj in traces
where MyFunction(obj.date_Closed.Date)
select obj;
或:
我不确定OP是否使用Linq to实体(它们提到了db),但如果它们是Date
,则无法工作。我最终使用了DbFunctions,因为EntityFunctions已被弃用。谢谢你的帮助!
public bool MyFunction(DateTime s)
{
DateTime d = DateTime.Parse(Your constructor);
return (s.date_Closed >= d);
}
var query = from obj in traces
where MyFunction(obj.date_Closed.Date)
select obj;
var query = traces.Where(p => MyFunction(p.data_Closed.Date));