Jon Bellamy

Jon Bellamy

  • NA
  • 47
  • 0

Linq -Return Unique Values and Sum of Another

Jul 3 2009 7:59 AM
Hi,

I have two tables:


1. Retailers
2. Invoices


Retailers has two columns:
1.1. RetailerID
1.2. RetailerName


Invoices has three columns:
2.1. InvoiceID
2.2. InvoiceProfit
2.3. RetailerID


Retailers.RetailerID is linked to Invoices.RetailerID (one-to-many).

What I want to do is write a linq (or in the form of a lambda exp) that returns Retailer.RetailerID, Retailer.RetailerName, Invoice.InvoiceProfit.

I can do this like so:

var retailers = from r in db.Retailers select t; var invoices = from i in db.Invoices select i; var retailersAndInvoices = from r in retailers join i in invoices on r.RetailerID equals i.RetailerID select new {t.RetailerName, i.InvoiceProfit};

However the snag I have is that I want to return only Distinct RetailerNames and the Sum of all Invoices.InvoiceProfit next to each one - the purpose being "Top Ten Retailers"!

I'm pulling my hair out trying to write the expression - does anyone know if you can do this?

Thanks - Jon

Answers (2)