Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/5.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
在Linq EF6中减去日期_Linq_Entity Framework_Date - Fatal编程技术网

在Linq EF6中减去日期

在Linq EF6中减去日期,linq,entity-framework,date,Linq,Entity Framework,Date,您好,我在Linq,EF6的减法方面有个大问题。 我有完成修理的日期。我想数一数还剩多少天 在ViewModel,我有: public TimeSpan TimeToLeft{get;set;} 在维修控制中心,我做了如下工作: var repairsToDo = from r in db.Repairs join c in db.Car on r.Car equals c.ID_Car join m in db.Models on c.ID_Modelu equals m.ID_Modelu

您好,我在Linq,EF6的减法方面有个大问题。 我有完成修理的日期。我想数一数还剩多少天

在ViewModel,我有:

public TimeSpan TimeToLeft{get;set;}
在维修控制中心,我做了如下工作:

var repairsToDo = from r in db.Repairs
join c in db.Car on r.Car equals c.ID_Car
join m in db.Models on c.ID_Modelu equals m.ID_Modelu
join b in db.Brand on m.ID_Brand equals b.Brand
where r.Data_Zakonczenia>=DateTime.Today 
select new RepairsToDo { TimeToLeft=(r.EndDate-DateTime.Today) };
dbarithmeticexpression arguments must have a numeric common type
视图:

我怎样才能修好它


编辑1:

控制器:

var today = DateTime.Today;
                var repairsToDo = from r in db.Repair
                                  join c in db.Car on r.Car equals c.ID_Car
                                  join m in db.Models on c.ID_Model equals m.ID_Model
                                  join b in db.Brand on m.ID_Brand equals b.ID_Brand
                                  where r.EndTime>=DateTime.Today
                                  select new { ... EndTime=r.EndTime };

                var model = repairsToDo.AsEnumerable().Select(raw => new RepairsToDo {...   TimeLeft= raw.EndTime- today });

return View(model);
错误:

The model item passed into the dictionary is of type 'System.Linq.Enumerable+WhereSelectEnumerableIterator`2[<>f__AnonymousType1a`7[System.Int32,System.String,System.String,System.String,System.String,System.String,System.DateTime],Praca_Inzynierska.Models.RepairsToDo]', but this dictionary requires a model item of type 'System.Linq.IQueryable`1[Praca_Inzynierska.Models.RepairsToDo]'.
    enter code here
传入字典的模型项的类型为'System.Linq.Enumerable+WhereSelectEnumerableInterator'2[f_uAnonymousType1A`7[System.Int32,System.String,System.String,System.String,System.String,System.DateTime],Praca_Inzynierska.Models.RepairsToDo],但此词典需要类型为“System.Linq.IQueryable`1[Praca_Inzynierska.Models.RepairsToDo]”的模型项。
在这里输入代码

最简单的方法可能是从EF获取数据,然后在本地执行算法:

var today = DateTime.Today;
var rawData = from r in db.Repairs
              join c in db.Car on r.Car equals c.ID_Car
              join m in db.Models on c.ID_Modelu equals m.ID_Modelu
              join b in db.Brand on m.ID_Brand equals b.Brand
              where r.Data_Zakonczenia >= DateTime.Today 
              select new { ..., r.EndDate };

var model = rawData.AsEnumerable() // Perform the select locally
                   .Select(raw => new RepairsToDo {
                               ... // other properties
                               TimeToLeft = raw.EndDate - today
                           });
请注意,我已经获取了
DateTime.Today
一次,而不是多次,这样即使在午夜前后执行此查询,也会得到一致的结果

我还建议将
TimeToLeft
重命名为
TimeLeft
RemainingTime

试试:

TimeToLeft = SqlFunctions.DateDiff("DAY", r.EndDate, DateTime.Now)
为你想要的任何单位换一天。请参见和。

使用EF6

System.Data.Entity.DbFunctions.DiffHours(time1,time2).Value
例如:

using System.Data.Entity;
...
entity.tableData.Select(m => new 
               {
                   m.Key,
                   horasTotales = m.Sum(h => DbFunctions.DiffHours(h.fecha_fin, h.fecha_inicio).Value)
               })

我喜欢您今天添加的
var
对于不在每次迭代中计算更有意义。
raw.EndDate-today
是否会产生时间跨度?OP请求天数,因此您可以将其括在括号中,并获得
部分,否?是的,这是时间跨度,因为这是减去两个日期的结果。但我需要的只是一个数字,例如2,5等@AdamLato:那么为什么你有
TimeSpan
属性?如果需要,您始终可以从
TimeSpan
中获取
Days
TotalDays
属性。。。但是您需要使您的模型属性具有您实际想要的类型…我尝试了:TimeToLeft=r.Data\u Zakonczenia.Subtract(DateTime.Today).Days,在ModelView中,我有公共int-TimeToLeft{get;set;},但我得到了错误:附加信息:LINQ-to-Entities无法识别方法'System.TimeSpan Subtract(System.DateTime)'方法,而此方法无法转换为存储表达式。@AdamLato:如果按照我的回答,在LINQ中对对象执行投影,则不会出现此错误。如果您更新您的问题以更改属性类型,我将更新我的答案…好的,这不是很清楚。当我运行应用程序并尝试使用此视图时,您从何处得到此错误?我认为您应该在帖子中添加
视图的相应部分。什么类型是
数据\u Zakonczenia
数据\u Zakonczenia=EndOfTimeVisual不知道System.Data.Objects上的对象是什么。SqlClient@AdamLato:你的评论没有多大价值有意义,但您是否有对
System.Data.Entity.dll的引用
?我只能添加System.Data.Entity,我没有dll选项。但是当我尝试编写SQLFunction时。。。visual不知道这种方法。您还需要一个
使用
指令来
System.Data.Objects.SqlClient
。。。您使用的是哪个版本的.NET?
using System.Data.Entity;
...
entity.tableData.Select(m => new 
               {
                   m.Key,
                   horasTotales = m.Sum(h => DbFunctions.DiffHours(h.fecha_fin, h.fecha_inicio).Value)
               })