简体   繁体   中英

Converting simple Sql query to Linq

Im working on my school project and im new in writing LINQ query's so its hard for me to convert this SQL query to LINQ. I have problem with writing linq query for top 10 best selling products. I have table for Products and table for Selling item.

在此处输入图片说明

My SQL query works good. 在此处输入图片说明

I tried with this:

  List<Proizvodi> proizvodi = db.Proizvodi.
            OrderByDescending(x => db.IzlazStavke.Where(y => y.ProizvodID == x.ProizvodID).
            Sum(t => t.Cijena)).Take(10).ToList();

But result is not good, any help please.

I'll try something like this:

List<Proizvodi> proizvodi = db.Proizvodi
        .OrderByDescending(x => db.IzlazStavke
             .Where(y => y.ProizvodID == x.ProizvodID)
             .GroupBy(a=>a.ProizvodID)
             .Select(grp=>grp.Sum(z=>z.Kolicina))
        .Take(10)
        .ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM