在Linq EF6中减去日期
您好,我在Linq,EF6的减法方面有个大问题。 我有完成修理的日期。我想数一数还剩多少天 在ViewModel,我有:在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
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)
})