2014-05-21 170 views
1

我有此查詢的SQL Server服務器做我想做的實體法.. 我怎樣才能將其轉換爲LINQ轉換SQL查詢到LINQ基於

select t1.IDHardware,h.DescricaoHardware 
from dbo.ProcessoHardware t1 
INNER JOIN 
(select t2.IDHardware, max(t2.IDProcessoHardware) as maxVisit 
from dbo.ProcessoHardware t2 
group by t2.IDHardware,t2.IDProcesso) v ON 
v.maxVisit = t1.IDProcessoHardware JOIN dbo.Hardware h ON t1.IDHardware=h.IDHardware 
where t1.Estado=1 AND IDProcesso=1 

這就是我現在......但我無法算出它過去這一點..

var ProcHardware = (from procHardware in db.ProcessoHardwares 
          where procHardware.IDProcesso == IDProcesso 
          select new { procHardware.IDHardware, procHardware.IDProcessoHardware, procHardware.IDProcesso, procHardware.Estado } into x 
          group x by new { x.IDHardware, x.IDProcesso, x.IDProcessoHardware, x.Estado } into t 
          let Max = t.Max(g => g.IDProcessoHardware) 
          select new { IDHardware = t.Key.IDHardware, Estado = t.Key.Estado, t.Key.IDProcesso,IDProcessoHardware=t.Key.IDProcessoHardware,cMax=Max }).ToList().Where(t => t.Estado == 1 && t.IDProcesso == IDProcesso && t.IDProcessoHardware==Max).Select(c => new VMProcessoChooseHardware 
         { 
          IDHardware = c.IDHardware 
         }); 

我有此表與表過程..這臺被稱爲processHardware涉及的表硬件。這個表由discribed:IDProcessHardware IDProcess IDHardware國家

領域狀態可以有3個狀態(1 - 插入,2-刪除,3-替代)..所以我可以我有此:

IDProcessHardware IDProcess IDHardware State 

    1     10   1  1 

    2     10   2  1 

    3     10   1  2 

    4     10   1  1 

    5     20   1  1 

我想要得到的是插入IDHardware但未從進程中刪除的IDHardware。 所以通過給IDProcess = 10我想與硬件ID 1和2的硬件..

IDProcessHardware IDProcess IDHardware State 

    1     10   1  1 

    2     10   2  1 

    3     10   1  2 

    4     20   1  1 
在表

以上通過給出IDProcess 10,它應該給我的硬件ID 2

在此先感謝...

回答

0

大量的試驗和錯誤之後,很多的搜索,我發現這個鏈接 http://jetmathew.wordpress.com/2014/01/21/select-latest-record-from-recordset-using-sql-and-linq/

的傢伙試圖類似於我想要什麼東西.. ,所以我挑LINQ查詢和改造它..

這就是我現在

var ProcHardware = (from a in db.ProcessoHardwares 
           group a by new { a.IDHardware, a.IDProcesso } into latest 
           join b in db.ProcessoHardwares on new { dt = latest.Max(itm => itm.IDProcessoHardware) } equals new { dt = b.IDProcessoHardware } 
           select new { ID = b.IDHardware, Estado=b.Estado,IDProcesso=b.IDProcesso }).ToList().Where(t => t.Estado == 1 && t.IDProcesso == IDProcesso).Select(c => new VMProcessoChooseHardware 
          { 
           IDHardware = c.ID 
          }); 

它仍然需要休息的信息discribing一樣的序列號,或者描述硬件。 我會在這裏發表完整的查詢..