25

Data is a local CSV file that is loaded into an ado.net dataset via OleDB. The table has 40+ columns consisting of invoice details. Each row is a separate line item within an invoice, which can consist of 1 to n rows.

The query is used to group the invoice details into a single row per invoice, totaling the invoice amount and balance due.

The following works, what I'm trying to determine: Is it possible to do this in a single query?

    //group the invoices by invoicenumber and sum the total
//Zoho has a separate record (row) for each item in the invoice
//first select the columns we need into an anon array   
var invoiceSum =
    DSZoho.Tables["Invoices"].AsEnumerable()
    .Select (x => 
        new {  
            InvNumber = x["invoice number"],
            InvTotal = x["item price"],
            Contact = x["customer name"],
            InvDate = x["invoice date"],
            DueDate = x["due date"],
            Balance = x["balance"],
            } );
    //then group and sum
    var invoiceTotals =
        invoiceSum
        .GroupBy (s => new {s.InvNumber, s.Contact, s.InvDate, s.DueDate} )
        .Select (g => 
            new {
                InvNumber = g.Key.InvNumber,
                InvDate = g.Key.InvDate,
                DueDate = g.Key.DueDate,
                Contact = g.Key.Contact,
                InvTotal = g.Sum (x => Math.Round(Convert.ToDecimal(x.InvTotal), 2)),
                Balance = g.Sum (x => Math.Round(Convert.ToDecimal(x.Balance), 2)),
                } );
2
  • 2
    You can place GroupBy right after first Select, without introducing invoiceTotals variable
    – Uriil
    Feb 22, 2014 at 14:15
  • possible duplicate of Group By Multiple Columns Apr 3, 2015 at 8:49

1 Answer 1

63

You are, in fact, only doing one query when you use the results of invoiceTotals. In the code you are showing you are even not doing a query on the database.

Google "linq deferred execution", it's nifty ;-)

But as Uriil says, you can just combine the statements into one linq query:

var invoiceSum =
DSZoho.Tables["Invoices"].AsEnumerable()
.Select (x => 
    new {  
        InvNumber = x["invoice number"],
        InvTotal = x["item price"],
        Contact = x["customer name"],
        InvDate = x["invoice date"],
        DueDate = x["due date"],
        Balance = x["balance"],
        }
 )
 .GroupBy (s => new {s.InvNumber, s.Contact, s.InvDate, s.DueDate} )
 .Select (g => 
        new {
            InvNumber = g.Key.InvNumber,
            InvDate = g.Key.InvDate,
            DueDate = g.Key.DueDate,
            Contact = g.Key.Contact,
            InvTotal = g.Sum (x => Math.Round(Convert.ToDecimal(x.InvTotal), 2)),
            Balance = g.Sum (x => Math.Round(Convert.ToDecimal(x.Balance), 2)),
            } 
 );
2
  • Ah- thanks for the help. 'linq deferred execution' Had not heard the term before either. I appreciate the input and help.
    – topry
    Feb 22, 2014 at 14:42
  • Whoo.Thats best of all answers i have ever got on Stack.(Y) Jul 18, 2014 at 7:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.