0

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.

enter image description here

My SQL query works good. enter image description here

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.

4
  • "But result is not good" - what exactly? Commented Jun 5, 2016 at 20:54
  • I got 10 products in list but not top 10 best selling products. Commented Jun 5, 2016 at 20:59
  • 1
    Why are you so surprised? In SQL query you get a sum of IzlazStavke.Kolicina and in Linq query you get Proizvodi.Cijena. Commented Jun 5, 2016 at 21:03
  • You're right mate. I spent hours, now it's working good. :) Commented Jun 5, 2016 at 21:10

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.