0

我有列的表:

ID(int), Ano(varchar), Bno(varchar), StartTime(DateTime), EndTime(DateTime)

此 SQL 查询的等效 LINQ 语句是什么:

 SELECT 
    StartTime, EndTime, Ano, Bno,  
    datediff(s, StartTime, EndTime) as duration 
 FROM 
    TABLENAME 
 where 
    StartTime >= 'value Input by user' 
    and StartTime <= 'value INPUT by user' 
 order by 
    StartTime desc

在这里,我如何选择将值转换为分钟的持续时间。以分钟和秒为单位计算所有记录的总持续时间

4

2 回答 2

2
DateTime startTime = <Startime given by user>;
DateTime endTime = <Endtime given by user>;

var result = 
yourSource
  .Where (m => m.StartTime >= startTime && m.StartTime<= endTime)
  .OrderByDescending(m => m.StartTime)
  .Select(m => new {
     startTime = m.StartTime,
     endTime = m.EndTime,
     ano = m.Ano,
     bno = m.Bno,
     duration = SqlFunctions.DateDiff("mi", m.StartTime, m.EndTime)
});

顺便说一句,你也可以得到没有持续时间的结果,并在检索结果后计算它(避免匿名)

就像是 :

var duration = (EndTime - StartTime).Minutes;
于 2012-08-29T08:51:26.837 回答
1

匹配以下代码:

var result = from Entityname in EntityListName
    where Entityname.StartTime >= 'value Input by user' etc ...
    orderby Entityname.StartTime descending 
    select Entityname;
于 2012-08-29T08:49:11.047 回答