在Linq EF6中减去日期

时间:2014-08-08 09:15:34

标签: linq entity-framework date

你好我在Linq,EF6的Subtract有很大的问题。 我有修理日期的日期。我想知道剩下多少天了。

在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) };

查看:

<table class="ShowDataTab">
        <tr>
            <th>Repair Number</th>
            <th>Car</th>
            <th>Name</th>
            <th>Desc</th>
            <th>Time to left</th>
        </tr>
        @foreach (var item in Model) { 
            <tr>
                <td>@item.FixNumber</td>
                <td>@item.Brand@item.Model</td>
                <td>@item.FixName</td>
                <td>@item.FixDesc</td>
                <td>@item.TimeToLeft</td>
            </tr>
        }

    </table>

我得到这样的错误:

dbarithmeticexpression arguments must have a numeric common type

我该如何解决?


EDIT1:

的Controler:

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

3 个答案:

答案 0 :(得分:1)

从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 一次,而不是多次执行 - 这样您就可以获得一致的结果,即使此查询是在午夜左右执行的。< / p>

我还建议将TimeToLeft重命名为TimeLeftRemainingTime

答案 1 :(得分:1)

尝试:

TimeToLeft = SqlFunctions.DateDiff("DAY", r.EndDate, DateTime.Now)

根据您想要的任何单位更改DAY。请参阅http://msdn.microsoft.com/en-us/library/dd487052(v=vs.110).aspxhttp://msdn.microsoft.com/en-us/library/ms189794.aspx

答案 2 :(得分:1)

使用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)
               })