0

如何选择持续时间作为实体框架中两个字段之间的差异。以下是相应的 SQL 查询:

SELECT InitiateTime, StartTime, EndTime, Ano,Bno,  datediff(s,StartTime,EndTime) as duration
FROM [NEPALIVR2].[dbo].[CDR]
WHERE StartTime >= startDate and StartTime <= endDate order by StartTime desc

LINQ

from u in db.CDRs 
where u.InitiateTime >= startDate && u.InitiateTime <= endDate 
orderby u.InitiateTime descending 
select new
{
   InitiateTime = u.InitiateTime,
   StartTime = u.StartTime,
   EndTime = u.StartTime,
   Ano = u.Ano,
   Bno = u.Bno,
   Duration = (SqlFunctions.DateDiff("mi", u.StartTime, u.EndTime))/360,
};

这将返回错误:

无法将类型隐式转换'System.Linq.IQueryable<AnonymousType#1>''System.Linq.IQueryable<IVRControlPanel.Models.CDR>'. 存在显式转换(您是否缺少演员表?)

4

1 回答 1

2

看起来您的问题不是差异计算,而是您从查询返回的匿名类型。将其更改为CDR对象:

select new CDR
{
   InitiateTime = u.InitiateTime,
   StartTime = u.StartTime,
   EndTime = u.StartTime,
   Ano = u.Ano,
   Bno = u.Bno,
   Duration = SqlFunctions.DateDiff("mi", u.StartTime, u.EndTime)/360
});
于 2013-02-27T06:17:44.127 回答