将sql查询转换为linq到基于实体的方法

时间:2014-05-21 16:34:56

标签: c# sql linq

我在sql server服务器中得到了这个查询,它做了我想要的... 我怎么能把它转换成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
                       });

我有这个表将表硬件与表Process相关联。这个表叫做processHardware。该表描述于:IDProcessHardware IDProcess IDHardware State

字段状态可以有3种状态(1-Insert,2-Remove,3-Substitute)..所以我可以拥有这个:

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。 所以通过给出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。

提前致谢...

1 个答案:

答案 0 :(得分: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
                           });

它仍然需要其余的信息来描述硬件,如序列号或描述。 我将在这里发布完整的查询..