LINQ - group/sum multiple columns

前端 未结 1 1170
挽巷
挽巷 2021-02-04 05:06

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 in

1条回答
  •  梦谈多话
    2021-02-04 05:46

    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)),
                } 
     );
    

    0 讨论(0)
提交回复
热议问题