Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.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/8/linq/3.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# 如何仅显示SystemDate小于/大于存储日期的数据库中的记录?_C#_Linq_Ado - Fatal编程技术网

C# 如何仅显示SystemDate小于/大于存储日期的数据库中的记录?

C# 如何仅显示SystemDate小于/大于存储日期的数据库中的记录?,c#,linq,ado,C#,Linq,Ado,我正在创建一个WPF C应用程序,允许您添加、删除和编辑租车订单,我正在尝试实现一个功能,仅从数据库中选择已完成的记录,或选择仍处于活动状态的记录 这是我对示例的完整方法 DateTime current = DateTime.Now; var query = from CarRental in this.dbContext.CarRentals where (CarRental.Starting_Date.AddDays(CarRental.Duration)) &

我正在创建一个WPF C应用程序,允许您添加、删除和编辑租车订单,我正在尝试实现一个功能,仅从数据库中选择已完成的记录,或选择仍处于活动状态的记录

这是我对示例的完整方法

  DateTime current = DateTime.Now;
            var query = from CarRental in this.dbContext.CarRentals where (CarRental.Starting_Date.AddDays(CarRental.Duration)) < current select CarRental;
            this.CarRentalViewSource.Source = query.ToList();
任何帮助都将不胜感激

谢谢

Jamie

EntityFramework将所有函数转换为sql,错误是EntityFramework无法将DateTime.AddDays函数转换为sql。您需要使用DbFunctions.AddDays而不是DateTime.AddDays,因此您的代码应该如下所示

DateTime current = DateTime.Now;
var query = from CarRental in this.dbContext.CarRentals where 
            (DbFunctions.AddDays(CarRental.Starting_Date, CarRental.Duration))) < current select CarRental;
this.CarRentalViewSource.Source = query.ToList();

您不是在与存储的日期进行比较,而是使用C方法AddDays来计算它。无法将其转换为SQL。这就是消息所说的。另一个问题是,通过计算日期,即使在SQL中这样做,也会阻止服务器使用任何包含起始日期的索引。这将强制进行全表扫描。创建一个结束日期的实际或持久计算字段,并将其添加到索引中。即使这样也不是一个好主意,因为它会阻止服务器使用任何覆盖开始日期字段的索引。这将起作用,但会导致一个完整的表scan@PanagiotisKanavos是的,你说得对。但我知道,在这种情况下,没有其他可行的方法。另一个解决方案是存储已计算的日期并查询该列,您在注释中已经提到了该列above@Ruben,您确定这是最新的代码吗?它似乎产生的错误多于修复错误
DateTime current = DateTime.Now;
var query = from CarRental in this.dbContext.CarRentals where 
            (DbFunctions.AddDays(CarRental.Starting_Date, CarRental.Duration))) < current select CarRental;
this.CarRentalViewSource.Source = query.ToList();