Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于方法将sql查询转换为linq to entities_C#_Sql_Linq - Fatal编程技术网

C# 基于方法将sql查询转换为linq to entities

C# 基于方法将sql查询转换为linq to entities,c#,sql,linq,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.IDProce

我在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
                       });
我得到了这个表,它将表硬件与表进程联系起来。。此表称为processHardware。此表由以下内容描述:IDProcessHardware IdProcessIdHardware State

字段状态可以有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。 因此,通过给定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


提前感谢…

经过大量的尝试和错误,以及大量的搜索,我找到了这个链接

那家伙在尝试类似我想要的东西。。 因此,我选择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
                           });
它还需要其他描述硬件的信息,如序列号或描述。 我将在这里发布完整的查询