Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在linq中比较日期_C#_Html_Forms_Linq_Linq To Entities - Fatal编程技术网

C# 在linq中比较日期

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

我想查询数据库中日期大于或等于给定日期的项目

数据库中的日期是日期时间并记录时间

如果用户输入搜索字符串“1/30/2014”,他们希望返回在该日期任何时间出现的条目。但是,任何在上午12点之后的时间都不会返回

我知道我可以简单地在搜索字符串中添加一天,但是有更合适的方法吗

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));