C# 使用Linq查询按日期搜索

C# 使用Linq查询按日期搜索,c#,json,asp.net-mvc,linq,datetime,C#,Json,Asp.net Mvc,Linq,Datetime,我有一个数据库表,它有一个字段调用CreatedDate,它是日期时间格式 例如:在该字段中,值的保存方式类似于2015-08-27 13:28:50.333或NULL 我编写了以下Linq查询,以按类型,类别,国家,子标准和日期进行搜索类型,类别,国家,子标准字段为nvarchar字段,日期为DateTime字段 [HttpGet] public JsonResult FetchProducts(string type, string category, string coun

我有一个数据库表,它有一个字段调用CreatedDate,它是日期时间格式

例如:在该字段中,值的保存方式类似于
2015-08-27 13:28:50.333
NULL

我编写了以下Linq查询,以按
类型
类别
国家
子标准
和日期进行搜索<代码>类型,
类别
国家
子标准
字段为nvarchar字段,日期为DateTime字段

    [HttpGet]
    public JsonResult FetchProducts(string type, string category, string country, string subsidary, string dateHERE)
    {

        DateTime? mydate = null;
        DateTime date2;
        bool check = DateTime.TryParse(dateHERE, out date2);
        if (check)
        {
            mydate = date2;
        }

        ................


        if (!string.IsNullOrWhiteSpace(type))
            products = products.Where(p => p.ProductType_ID.StartsWith(type));

        ....................

        if (check)
        {
           products = products.Where(p => p.CreatedDate.Equals(mydate)).ToList();
        }

        ...............

        return Json(data, JsonRequestBehavior.AllowGet);
    }
一旦我输入日期和搜索,我就得到空值


此查询适用于除日期之外的几乎所有字段。什么是正确的linq查询以搜索日期

您必须截断时间:

 ...products.Where(p => EntityFunctions.TruncateTime(p.CreatedDate) == mydate)
如果您使用的是EF6,则将
EntityFunctions
更改为
DbFunctions

其他信息:

方法称为标准函数。这些是所有实体框架提供者都支持的一组函数。这些规范函数将被转换为提供程序的相应数据源功能。规范函数是访问核心语言之外的功能的首选方式,因为它们保持查询的可移植性

您可以找到所有规范函数以及所有日期和时间规范函数

不要忘记添加对
System.Data.Objects
System.Data.Entity
的引用

或者另外您可以使用该逻辑截断时间部分:

DateTime? myDateTomorrow = myDate;
myDateTomorrow.Value.AddDays(1);

products.Where(p => p.CreatedDate >= mydate && p.CreatedDate < myDateTomorrow);
DateTime?myDate明天=myDate;
MyDateTomother.Value.AddDays(1);
products.Where(p=>p.CreatedDate>=mydate&&p.CreatedDate
您必须截断时间:

 ...products.Where(p => EntityFunctions.TruncateTime(p.CreatedDate) == mydate)
如果您使用的是EF6,则将
EntityFunctions
更改为
DbFunctions

其他信息:

方法称为标准函数。这些是所有实体框架提供者都支持的一组函数。这些规范函数将被转换为提供程序的相应数据源功能。规范函数是访问核心语言之外的功能的首选方式,因为它们保持查询的可移植性

您可以找到所有规范函数以及所有日期和时间规范函数

不要忘记添加对
System.Data.Objects
System.Data.Entity
的引用

或者另外您可以使用该逻辑截断时间部分:

DateTime? myDateTomorrow = myDate;
myDateTomorrow.Value.AddDays(1);

products.Where(p => p.CreatedDate >= mydate && p.CreatedDate < myDateTomorrow);
DateTime?myDate明天=myDate;
MyDateTomother.Value.AddDays(1);
products.Where(p=>p.CreatedDate>=mydate&&p.CreatedDate
您必须截断时间:

 ...products.Where(p => EntityFunctions.TruncateTime(p.CreatedDate) == mydate)
如果您使用的是EF6,则将
EntityFunctions
更改为
DbFunctions

其他信息:

方法称为标准函数。这些是所有实体框架提供者都支持的一组函数。这些规范函数将被转换为提供程序的相应数据源功能。规范函数是访问核心语言之外的功能的首选方式,因为它们保持查询的可移植性

您可以找到所有规范函数以及所有日期和时间规范函数

不要忘记添加对
System.Data.Objects
System.Data.Entity
的引用

或者另外您可以使用该逻辑截断时间部分:

DateTime? myDateTomorrow = myDate;
myDateTomorrow.Value.AddDays(1);

products.Where(p => p.CreatedDate >= mydate && p.CreatedDate < myDateTomorrow);
DateTime?myDate明天=myDate;
MyDateTomother.Value.AddDays(1);
products.Where(p=>p.CreatedDate>=mydate&&p.CreatedDate
您必须截断时间:

 ...products.Where(p => EntityFunctions.TruncateTime(p.CreatedDate) == mydate)
如果您使用的是EF6,则将
EntityFunctions
更改为
DbFunctions

其他信息:

方法称为标准函数。这些是所有实体框架提供者都支持的一组函数。这些规范函数将被转换为提供程序的相应数据源功能。规范函数是访问核心语言之外的功能的首选方式,因为它们保持查询的可移植性

您可以找到所有规范函数以及所有日期和时间规范函数

不要忘记添加对
System.Data.Objects
System.Data.Entity
的引用

或者另外您可以使用该逻辑截断时间部分:

DateTime? myDateTomorrow = myDate;
myDateTomorrow.Value.AddDays(1);

products.Where(p => p.CreatedDate >= mydate && p.CreatedDate < myDateTomorrow);
DateTime?myDate明天=myDate;
MyDateTomother.Value.AddDays(1);
products.Where(p=>p.CreatedDate>=mydate&&p.CreatedDate
这里的每个人都已经提到了您的日期
.Equals()
由于毫秒原因无法工作,我通常使用
DateTime
类的
date
属性,该属性只获取datatime对象的日期部分,因此我建议您这样做:

if (check)
{
    products = products.Where(p => p.CreatedDate != null //check on null
    && p.CreatedDate.Value.Date == mydate).ToList();
}

这里的每个人都提到了您的日期
.Equals()
由于毫秒数而无法工作,因此我通常使用
DateTime
类的
date属性,该属性只获取datatime对象的日期部分,因此我建议您这样做:

if (check)
{
    products = products.Where(p => p.CreatedDate != null //check on null
    && p.CreatedDate.Value.Date == mydate).ToList();
}

这里的每个人都提到了您的日期
.Equals()
由于毫秒数而无法工作,因此我通常使用
DateTime
类的
date属性,该属性只获取datatime对象的日期部分,因此我建议您这样做:

if (check)
{
    products = products.Where(p => p.CreatedDate != null //check on null
    && p.CreatedDate.Value.Date == mydate).ToList();
}

这里的每个人都提到了您的日期
.Equals()
由于毫秒数而无法工作,因此我通常使用
DateTime
类的
date属性,该属性只获取datatime对象的日期部分,因此我建议您这样做:

if (check)
{
    products = products.Where(p => p.CreatedDate != null //check on null
    && p.CreatedDate.Value.Date == mydate).ToList();
}

如果您的查询来自SQL后端,您必须始终记住,数据库引擎应该能够将生成的SQL转换为高效的ex