需要为LINQ创建此查询

时间:2014-04-10 01:53:51

标签: c# linq entity-framework

晚安,

如果你能帮助我,我很难在LINQ中转换这个查询

谢谢

SELECT  DISTINCT STA.ID_OBRA, 
        STA.ID_AREA, 
        STA.ID_FLUXO 
FROM        TB_WF_STATUS_APROVACAO STA  
WHERE   STA.ID_OBRA NOT IN (  
            SELECT  ID_OBRA 
            FROM    TB_WF_STATUS_APROVACAO STA 
            WHERE   STA.SEQUENCIA = (   SELECT  MAX(SEQUENCIA) 
                                        FROM    TB_WF_STATUS_APROVACAO 
                                        WHERE   ID_FLUXO = STA.ID_FLUXO) 
                                            OR  STA.APROVADO <> 1 
                                        GROUP BY ID_OBRA)

我正在使用Entity Framework,我尝试过这个:

var q = (from a in db.pendenteAprovacao.Where(a => !aprovacaoList .SelectMany(b => b.id_obra);

2 个答案:

答案 0 :(得分:2)

这是一个接近1对1的翻译(未经测试):

var query =
    (from sta1 in db.TB_WF_STATUS_APROVACAO
    let subQuery =
        from sta2 in db.TB_WF_STATUS_APROVACAO
        let maxSequencia =
            (from sta3 in db.TB_WF_STATUS_APROVACAO
            where sta3.ID_FLUXO == sta2.ID_FLUXO
            select sta3.SEQUENCIA).Max()
        where sta2.SEQUENCIA == maxSequencia || sta2.APROVADO != 1
        group sta2 by sta2.ID_OBRA into g
        select g.Key
    where !subQuery.Contains(sta1.ID_OBRA)
    select new
    {
        sta1.ID_OBRA,
        sta1.ID_AREA,
        sta1.ID_FLUXO,
    }).Distinct();

答案 1 :(得分:0)

回复:

        var q = (from STA in aprovacaoList
                 where
                   !
                     (from STA0 in aprovacaoList
                      where
                        STA0.sequencia ==
                          (from TB_WF_STATUS_APROVACAO in aprovacaoList
                           where
                             TB_WF_STATUS_APROVACAO.id_fluxo == STA0.id_fluxo
                           select new
                           {
                               TB_WF_STATUS_APROVACAO.sequencia
                           }).Max(p => p.sequencia) ||
                        STA0.aprovado != true
                      group STA0 by new
                      {
                          STA0.id_obra
                      } into g
                      select new
                      {
                          Id_obra = (Int32?)g.Key.id_obra
                      }).Contains(new { Id_obra = (Int32?)STA.id_obra })
                 select new
                 {
                     STA.id_obra,
                     STA.id_area,
                     STA.id_fluxo
                 }).Distinct();