如何从DbFunctions.DiffMinutes()获得准确的小时和分钟

时间:2016-04-19 06:55:02

标签: c# linq entity-framework-6

我正在使用这个linq查询,它工作正常。唯一的问题是它是返回小时数,即10,9等,但我想要完整的小时数和分钟数,即10:28,9:45等

DayHours = (from ab in db.Attendances
               where ab.Employee == 63
                   && ab.InTime.Value.Year == 2015
                   && ab.InTime.Value.Month == i
               select new
               {
                   Day = ab.InTime.Value.Day,
                   Hours = DbFunctions.DiffMinutes(ab.InTime, ab.OutTime) / 60
               }); 

2 个答案:

答案 0 :(得分:2)

你可以试试这个:

/*...*/
      select new
      {
          Day = ab.InTime.Value.Day,
          Hours = DbFunctions.DiffMinutes(ab.InTime, ab.OutTime) / 60
          Minutes = DbFunctions.DiffMinutes(ab.InTime, ab.OutTime) % 60                   
       }); 

如果您不想使用此格式,请准确说明您希望如何存储它(Timespan变量?还有其他内容?)

答案 1 :(得分:-1)

您可以从DB获得准确时间

select round(
    Cast(
        DateDiff(
            MINUTE, 
            convert(varchar(5), '09:40:00.0000000', 108),
            convert(varchar(5), '18:12:00.0000000', 108)
        ) / 60 as float
    )
    + Cast(
        DateDiff(
            MINUTE, 
            convert(varchar(5), '09:40:00.0000000', 108),
            convert(varchar(5), '18:12:00.0000000', 108)
        ) % 60 as float
    ) / 60, 2
) as [Hour in office]