2015-06-27 66 views
1

在stackoverflow上多次詢問如何在LINQ中將日期時間格式化爲sql表達式,並且解決方案總是首先使用ToList()強制編譯linq,然後使用.ToString(「hh:mm」)格式化選項的豪華處理日期時間。Linq to SQL Format在一次拍攝中的日期時間

但我試圖做到一個拿,我部分成功,除了代碼是可怕和醜陋的,任何更短的方式來做到這一點,假設日期對象是存儲在數據庫中的unix時間戳,即時嘗試只返回時間部分4:54 pm

  production_cycles = from p in db.ProductionCycles 
           where p.IsRunning == true 
           select new Rest.ProductionCycle { 
            id = p.ID, 
            name = p.Name, 
            created = p.Created, 
            steps = from s in p.Logs 
              where user_permissions.Contains(s.Permission.ID) 
              orderby s.ID ascending 
              select new Rest.Step { 
               created = s.Created, 
               created_label = DbFunctions.CreateDateTime(
          (int)SqlFunctions.DatePart("yyyy", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))), 
          (int)SqlFunctions.DatePart("m", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))), 
          (int)SqlFunctions.DatePart("d", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))), 
          (int)SqlFunctions.DatePart("hh", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))), 
          (int)SqlFunctions.DatePart("mi", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))), 
          0 

         ).Value.Hour.ToString() 
          + ":" + 
          DbFunctions.CreateDateTime(
          (int)SqlFunctions.DatePart("yyyy", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))), 
          (int)SqlFunctions.DatePart("m", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))), 
          (int)SqlFunctions.DatePart("d", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))), 
          (int)SqlFunctions.DatePart("hh", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))), 
          (int)SqlFunctions.DatePart("mi", (DateTime)SqlFunctions.DateAdd("ss", s.Created + (offset * 60 * 60), new DateTime(1970, 1, 1, 0, 0, 0, 0))), 
          0 

         ).Value.Minute.ToString()       


          , 
               username = s.User.Name, 
+0

我會把醜陋的代碼在一個單獨的方法,而不是搞亂你LINQ。 unix時間戳在1970年開始,而窗口在1900年開始。所以你可以將1900年1月1日到1970年1月1日的時間加到unix時間。 – jdweng

回答

1

請嘗試了這一點:

var baseDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); 
production_cycles = from p in db.ProductionCycles 
    where p.IsRunning == true 
    select new Rest.ProductionCycle 
    { 
     id = p.ID, 
     name = p.Name, 
     created = p.Created, 
     steps = from s in p.Logs 
      where user_permissions.Contains(s.Permission.ID) 
      orderby s.ID ascending 
      select new Rest.Step 
      { 
       created = s.Created, 
       created_label = DbFunctions.DiffHours(baseDate, s.Created).ToString + ":" + DbFunctions.DiffMinutes(baseDate, s.Created).ToString + ":" +DbFunctions.DiffSeconds(baseDate, s.Created).ToString 

       ////// the rest as usual 

      }, 
    };