Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.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# 我的EF查询无法返回c中的日期时间_C#_Sql Server_Entity Framework_Datetime - Fatal编程技术网

C# 我的EF查询无法返回c中的日期时间

C# 我的EF查询无法返回c中的日期时间,c#,sql-server,entity-framework,datetime,C#,Sql Server,Entity Framework,Datetime,我有这个数据库,你可以看到: private void lblSubmitDate_EditValueChanged(object sender, EventArgs e) { DateTime subDateTime = Convert.ToDateTime(lblSubmitDate.EditValue).Date; gridControlDocument.DataSource = new BindingList<Docume

我有这个数据库,你可以看到:

private void lblSubmitDate_EditValueChanged(object sender, EventArgs e)
{
  DateTime subDateTime = Convert.ToDateTime(lblSubmitDate.EditValue).Date;
            gridControlDocument.DataSource =
                new BindingList<Document>(_documentRepository
                .Get()
                .Where(i => i.SubmitDateTime==subDateTime)
                .ToList())
                { AllowNew = true };
}
在我的代码中,我想返回一些带有日期时间条件的记录,如您所见:

private void lblSubmitDate_EditValueChanged(object sender, EventArgs e)
{
  DateTime subDateTime = Convert.ToDateTime(lblSubmitDate.EditValue).Date;
            gridControlDocument.DataSource =
                new BindingList<Document>(_documentRepository
                .Get()
                .Where(i => i.SubmitDateTime==subDateTime)
                .ToList())
                { AllowNew = true };
}

您将永远不会检索任何值,因为您正在解析DateTime,但只获取日期部分。这意味着时间部分将为00:00:00:

DateTime subDateTime = Convert.ToDateTime(lblSubmitDate.EditValue).Date;
当您尝试选择行时,您将与具有时间组件的SubmitDateTimes进行比较:

gridControlDocument.DataSource = new BindingList<Document>(_documentRepository.Get().Where(i => i.SubmitDateTime==subDateTime).ToList()) { AllowNew = true };

您需要将数据库字段中的日期部分与用户选择中解析的日期进行比较。

您将永远无法检索任何值,因为您正在解析日期时间,但只获取日期部分。这意味着时间部分将为00:00:00:

DateTime subDateTime = Convert.ToDateTime(lblSubmitDate.EditValue).Date;
当您尝试选择行时,您将与具有时间组件的SubmitDateTimes进行比较:

gridControlDocument.DataSource = new BindingList<Document>(_documentRepository.Get().Where(i => i.SubmitDateTime==subDateTime).ToList()) { AllowNew = true };

您需要将数据库字段中的日期部分与用户选择中解析的日期进行比较。

Josh所说的是正确的


您将永远不会检索任何值,因为您正在分析 DateTime,但只取日期部分。这意味着时间 部分为00:00:00:

DateTime subDateTime = Convert.ToDateTime(lblSubmitDate.EditValue).Date;
必须使用EntityFunction.TruncateTime才能仅获取日期

gridControlDocument.DataSource = 
          new BindingList<Document>(_documentRepository
          .Get()
          .Where(i => EntityFunctions.TruncateTime(i.SubmitDateTime)==subDateTime)
          .ToList()) { AllowNew = true };

乔希说的是对的


您将永远不会检索任何值,因为您正在分析 DateTime,但只取日期部分。这意味着时间 部分为00:00:00:

DateTime subDateTime = Convert.ToDateTime(lblSubmitDate.EditValue).Date;
必须使用EntityFunction.TruncateTime才能仅获取日期

gridControlDocument.DataSource = 
          new BindingList<Document>(_documentRepository
          .Get()
          .Where(i => EntityFunctions.TruncateTime(i.SubmitDateTime)==subDateTime)
          .ToList()) { AllowNew = true };

subDateTime是SubmitDateTime列中的一个值吗?aka,它应该通过找到一个等价项来返回行吗?@MarkC。subdatetime是用户可以从datetimepickerRight中选择的值,但是subdatetime的值是多少?如果您有SQL Server配置文件-EF生成的查询是什么?@MarkC。让我检查一下sqlprofiler。很高兴你发现SubmitDateTime列中的subDateTime是一个值?aka,它应该通过找到一个等价项来返回行吗?@MarkC。subdatetime是用户可以从datetimepickerRight中选择的值,但是subdatetime的值是多少?如果您有SQL Server配置文件-EF生成的查询是什么?@MarkC。让我检查一下sqlprofiler。很高兴您发现了EntityFramework.SqlServer.dll中发生了类型为“System.NotSupportedException”的未处理异常。其他信息:指定的类型成员“Date”在LINQ to Entities中不受支持。只支持初始值设定项、实体成员和实体导航属性。DateTime.Date目前无法在SQL语句中转换,因此,是的,除非您枚举了结果,否则无法使用.Date。在这种情况下,您查询的是对象数据集,而不是数据库。我可以发誓他们添加了该绑定。从我的答案中删除了该选项,以便不会有人感到困惑。EntityFramework.SqlServer.dll中发生了类型为“System.NotSupportedException”的未处理异常。其他信息:LINQ to Entities中不支持指定的类型成员“Date”。只支持初始值设定项、实体成员和实体导航属性。DateTime.Date目前无法在SQL语句中转换,因此,是的,除非您枚举了结果,否则无法使用.Date。在这种情况下,您查询的是对象数据集,而不是数据库。我可以发誓他们添加了该绑定。从我的答案中删除了这一点,以便没有人会感到困惑。注意:根据使用的Entity Framework的版本,可能必须使用DbFunctions,而不是EntityFunctions,如前所述。注意:根据使用的Entity Framework的版本,可能必须使用DbFunctions,而不是EntityFunctions,如前所述。