Okay so I have a listview control with 3 columns:
Year | NetTotal | GrossTotal
also i have a table called Orders with several columns, they contain information about the order and store the ID of the Customer they belong to.
Question is: how can i query that table with (preferably) linq (the datacontext is from LinqToSql) to return the following data?
I want to search for any entry with the matching CustomerID which took place, group them by Year, Sum the Totals respectively and add them to the listview?
I now i could use lamda expressions and aggregate, its just not clear how (option infer on,db is a datacontext object,CustomerID is an int32 variable):
Dim Orders = (From order In db.Orders Where order.CustomerID = CustomerID).GroupBy(Function(p) p.Date.Year).GetEnumerator
I reckon i’d have to create an anonymous type like the following:
Dim tmpYears = From prevs In db.Orders Select New With {.CustID = prevs.CustomerID, .Year = prevs.PaymentDate.Year, .NetPurchase, .GrossPurchase}
But how do I aggregate the Purchased column in a group?
Dim CustomerOrders = From ord In db.Orders Where Ord.CustomerID = custID Select ord
Dim tot = From O in CustomerOrders Select Aggregate netTot In O Into Sum(netTot.Price * netTot.Quantity * 1+ (netTot.Discount/100))
I want to merge the two.
Any suggestions? (I’ve read this but i want it in Linq because its a team project and we agreed on using Linq instead of sending .ExecuteQuerys and etc to the db.Also its a LinqToSQL solution so would be better if i could make some use of it)
I can’t guarantee I understand your requirements exactly, but long story short it seems you want to display orders, grouped by year, with the aggregated sums for net/gross value, where the orders match a provided CustomerID?
Sorry if the syntax is slightly out but I’m doing this freehand…
This should provide you an anonymous type with
Year,NetTotal, andGrossTotalpopulated as per the requirements I listed.EDIT: Also apologies for the one liner but I’m sure you can reformat it to taste.