选择计算字段

时间:2013-02-27 06:11:48

标签: c# linq entity-framework compiler-errors linq-to-entities

如何选择持续时间作为实体框架中两个字段之间的差异。以下是相应的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>'。存在显式转换(您是否错过了演员?)

1 个答案:

答案 0 :(得分: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
});