none
Max Group Linq to Sql RRS feed

  • Question

  •  

    How can i write this sql in linq

     

    select * from praca where praca_id in (select max(praca_id) from praca where pais_codigo = 1 and praca_dataatualizacao <= getDate() group by pais_codigo, praca_codigo) and praca_datatermino = '19000101' and pais_codigo = 1

     

    I have to group the object praca and get the maix id, whith that i can run the external sql

     

    i try this but didnt work

     

    DateTime now = DateTime.Now;

    var query =

    (

    from p in dbSiscam.Pracas

    where p.PAIS_Codigo == 1

    && p.PRACA_DataTermino == Convert.ToDateTime("1900-01-01")

    && p.PRACA_DataAtualizacao <= now

    && (from praca in dbSiscam.Pracas

    where praca.PAIS_Codigo == 1

    group praca by new { praca.PRACA_Codigo, praca.PAIS_Codigo } into pracaGroup

    select new { PRACA_ID = pracaGroup.Max(praca => praca.PRACA_ID) }).Contains(p.PRACA_ID)

    select p

    ).OrderByDescending(p => p.PRACA_ID);

     

     

    anyone can help me

    3 days trying to solve this

     

    []´s

    Thursday, October 9, 2008 5:11 PM

Answers

  •  

    The types of the expression in "new { PRACA_ID = .. }" and "p.PRACA_ID" don't match. 

     

    instead of

    select new { PRACA_ID = pracaGroup.Max(praca => praca.PRACA_ID) }

     

    you should have

     

    select pracaGroup.Max(praca => praca.PRACA_ID)

     

     

    Thursday, October 9, 2008 5:30 PM
    Moderator