none
Linq to sql Group by and having trouble RRS feed

  • Question

  • Hi there; 

    Here is  my sql query

     

     SELECT CariKod AS DepoNo,max(CariNam) as DepoAd,max(StokNam) as StokAdi,max(StokKod) AS StokKodu,

    TOPLAMMIKTAR= sum( case when SthIslem='+' then SthMiktar end),

    MIKTAR = sum( case when SthIslem='+' then SthMiktar else -SthMiktar end)

    FORM StokHareket  join Stoklar on ( SthStok = StokIdent)  join Cariler on ( CariIdent=SthDepo)

     GROUP BY CariKod,StokKod HAVING MAX(CariTip) = 3  

     

    Can anyone help me to convert linq ?  I dont know how i can use group by and having in linq together. Ty

    Friday, June 11, 2010 8:39 AM

Answers

  • Hi,

    Your query does not give any ideas about what fields refer to what tables but this should give you some ideas:

    from sh in dc.StokHareket
    join s in dc.Stoklar on sh.stokIdent equals s.SthStok
    join c in dc.Cariler on sh.SthDepo equals c.CarIdent
    
    group new { sh,s,c } by new { c.CariKod, s.stokKod } into grp
    where grp.Max(c=>c.CariTip) = 3
    select new
    {
     DepoNo = grp.Key.CariKod,
     grp.Max( c=>c.CariNum ),
     grp.Where(g=>g.s.sthIslem == '+').Sum( s=>s.SthMiktar )
     ...
    }
    

     

    Notice that I am creating an anonymous type for grouping as described here to get all values from different tables.

     

    Regards,


    Syed Mehroz Alam
    My Blog | My Articles
    • Marked as answer by Can Altunöz Friday, June 11, 2010 11:04 AM
    Friday, June 11, 2010 10:04 AM