2014-01-23 100 views
-1

我想將我的續集指令轉換成Linq指令。 數據庫上下文名爲contestoDB我如何將我的SQL命令轉換爲linq命令?

這是我的SQL查詢:

SELECT 
    DATEDIFF(DD, GETDATE(), CERTIFICATI_2.DATA_SCADENZA) AS DAYSTOGO, 
    CERTIFICATI_2.DATA_SCADENZA, AREE.NOME_AREA, 
    AGENTS.MACCHINA, AGENTS.ALIAS_AGENT, NUMBEFOR_ALERTDAYS, 
    AGENTS.USER_ABILIATI, UTENTI_2.UTENTE as RESPONSABILE, UTENTI_2.EMAIL 
FROM 
    CERTIFICATI_2 
INNER JOIN 
    AGENTS ON CERTIFICATI_2.ID_AGENTS = AGENTS.ID_AGENT 
INNER JOIN 
    UTENTI_2 ON AGENTS.RESPONSABILE = UTENTI_2.ID 
INNER JOIN 
    AREE ON AGENTS.ID_AREA = AREE.ID_AREA 
WHERE 
    DATEDIFF(DD,GETDATE(),CERTIFICATI_2.DATA_SCADENZA) = ISNULL(NUMBEFOR_ALERTDAYS,57) 
    OR DATEDIFF(DD,GETDATE(),CERTIFICATI_2.DATA_SCADENZA) = 0 
ORDER BY 
    CERTIFICATI_2.DATA_SCADENZA 

有沒有什麼地方誰幫我Linq中的轉換?

謝謝!

PS:我試過Linquer,但它不會轉換我的sql,因爲ISNULLDATEDIFF會在轉換和關閉程序時產生問題。

+0

請永遠只是下降的SQL,並要求轉換。至少顯示一個類模型,以便導航屬性和關聯的多樣性是可見的。並展示你自己的第一個努力。 –

+0

http://stackoverflow.com/questions/12238423/linqpad-convert-sql-to-linq-command這個問題之前已被問過。 –

+0

你可以使用http://www.linqpad.net/codesnippetide.aspx,如果你總是想從SQL轉換=> Linq –

回答

0

這應該工作:

var query = from cert in db.Certificati 
      join agent in db.Agents 
      on cert.Id_Agents equals agent.Id_Agent 
      join utenti in db.Utenti 
      on agent.Responsabile equals utenti.ID 
      join aree in db.Aree 
      on agent.Id_Area equals aree.Id_Area 
      where (DateTime.Now - cert.Data_Scadenza).TotalDays == 0 
       || (DateTime.Now - cert.Data_Scadenza).TotalDays == cert.NUMBEFOR_ALERTDAYS ?? 57 
      orderby cert.Data_Scadenza descending 
      select new 
      { 
       DaysToGo = (DateTime.Now - cert.Data_Scadenza).TotalDays, 
       // other columns 
      };