views:

669

answers:

4

I'm unable to convert this SQL query into a working linq statement

select sum(cena), id_auta, max(servis) from dt_poruchy left outer join mt_auta on dt_poruchy.id_auta=mt_auta.id
where dt_poruchy.servis>=3 group by id_auta;

I tryed something like this but i cant handle the select statement

   var auta = from a in MtAuta.FindAll()
                   join p in DtPoruchy.FindAll() on a equals p.MtAuta into ap
                   from ap2 in ap.DefaultIfEmpty()
                   where ap2.SERVIS >= 3
                   group ap2 by ap2.ID into grouped
                   select new {

I'll appreciate any help!

+1  A: 

I am not sure which table cena and servis are coming from but to create grouped sum you do something like.

select new { Sum = grouped.Sum( x => x.cena ) }

and to get max

select new { Max = grouped.Group.Max( x => x.servis ) }

Here is a good reference for you.

Stan R.
+2  A: 

Based on the limited information provided (which tables are certain fields from?), here is what I came up with.

var auta = from a in MtAuta.FindAll()
           let p = a.DtPoruchys.Where(s => s.SERVIS >= 3)
           select new
           {
               Id = a.Id,
               CenaSum = p.Sum(c => c.Cena),
               Servis = p.Max(s => s.SERVIS)
           };
Ryan Versaw
Let me know if I made any incorrect assumptions, and I'll make sure to update my answer!
Ryan Versaw
i'm sorry for my incoplete definition.The MtAuta is the master table (containing DtPoruchy) and DtPoruchy is the slave table containing the CENA,SERVIS and ID_AUTA fields.
Besides the capitalization in my answer, does this work for you? Are you using LinqToSql, and if so, are your relationships set up properly? It appeared so, as you had `p.MtAuta` in your question, but it might be good to confirm.
Ryan Versaw
actually i'm not using Linq to SQL but Linq to NHibernate. Your answer didn't work for me. And the class DtPoruchy has a reference to the MtAuto. The statement p.MtAuta is correct.
I would imagine a simpler solution may exist (similar to mine), but I haven't played with Linq to NHibernate yet, so I don't know what it is :(
Ryan Versaw
+1  A: 

I've reached this solution (supposing "cena" belongs to MtAuta.FindAll()):

        var auta = from e in
                       (from a in DtPoruchy.FindAll()
                        where a.SERVIS >= 3
                        join p in MtAuta.FindAll() on a.MtAuta equals p.Id into ap
                        from ap2 in ap.DefaultIfEmpty()
                        select new
                        {
                            Cena = ap.cena,
                            IdAuta = a.MtAuta,
                            Servis = a.servis
                        })
                   group e by e.IdAuta into g
                   select new
                   {
                       Cena = g.Sum(e => e.cena),
                       IdAuta = g.Key,
                       Servis = g.Max(e => e.servis)
                   };
bruno conde
A: 

I've modified your solution little bit and i got it working like this:

var auta = from jo in
                       (
                           from a in MtAuta.FindAll()
                           join p in DtPoruchy.FindAll() on a equals p.MtAuta into ap
                           from ap2 in ap.DefaultIfEmpty()
                           where ap2.SERVIS >= 3
                           select new
                           {
                               Cena = ap2.CENA,
                               Idauto = ap2.ID_AUTA,
                               Servis = ap2.SERVIS
                           }
                       )
                   group jo by jo.Idauto into g
                   select new
                   {
                       Cena = g.Sum(jo => jo.Cena),
                       IdAuto = g.Key,
                       Servis = g.Max(jo => jo.Servis)
                   };

I just curious if this is the best solution?

It's unlikely that you need anything remotely this complicated. Check out Ryan's answer.
Joe Albahari