var results = (Context.invoices // Start at invoices because this is the list we really care about .Include(inv => inv.client) // Eager load the client so it doesn't require additional queries .Where(inv => inv.invoiceamount >= InvoiceAmount) .OrderByDescending(x => x.invoiceamount)// Filter results .GroupBy(inv => inv.client) // Group results by client so we can loop through the clients .ToList()); // Execute the querySample output
Company: Steve Hall Fire Protection (id: 8) Invoice reference: PJK\Steve Hall Fire Prevention\30-06-2014-0007 Invoice date: 30/06/2014 Invoice amount: £20,000,000.00 Charge type: Daily Rate Invoice reference: PJK\Steve Hall Fire Prevention\30-06-2014-0008 Invoice date: 30/06/2014 Invoice amount: £20,000,000.00 Charge type: Daily Rate Invoice reference: PJK\Steve Hall Fire Prevention\27-06-2014-0003 Invoice date: 27/06/2014 Invoice amount: £6,000,000.00 Charge type: Daily Rate Invoice reference: PJK\Steve Hall Fire Prevention\30-06-2014-0005 Invoice date: 30/06/2014 Invoice amount: £20,000,000.00 Charge type: Daily Rate Invoice reference: PJK\Steve Hall Fire Prevention\30-06-2014-0006 Invoice date: 30/06/2014 Invoice amount: £20,000,000.00 Charge type: Daily Rateas you can see there's a 6,000,000.00 snuck in the middle - the same thing happens on all my test data.