From the Data,I have to find datewise total and GrandTotal the appropriate row should be
filled with (***) when not applicable).
OrderID OrderAmt OrderDate
----------- ---------- --------------------
1 10.50 2003-10-11 08:00:00
2 11.50 2003-10-11 10:00:00
3 1.25 2003-10-11 12:00:00
4 100.57 2003-10-12 09:00:00
5 19.99 2003-10-12 11:00:00
6 47.14 2003-10-13 10:00:00
7 10.08 2003-10-13 12:00:00
8 7.50 2003-10-13 19:00:00
9 9.50 2003-10-13 21:00:00
Output
OrderID Order Date OrderAmt Sub Total Grand Total
----------- ---------- ---------- ---------- -----------
1 10/11/2003 10.50 *** ***
2 10/11/2003 11.50 *** ***
3 10/11/2003 1.25 23.25 ***
4 10/12/2003 100.57 *** ***
5 10/12/2003 19.99 120.56 ***
6 10/13/2003 47.14 *** ***
7 10/13/2003 10.08 *** ***
8 10/13/2003 7.50 *** ***
9 10/13/2003 9.50 74.22 218.03
What are the changes needed in the following query ?
var qry =
(from o in ord
select new
{
OrdNumber = o.OrderNumber,
OrdDate = o.OrderDate,
Amount = o.OrderAmount,
GrandTotal = ord.Sum(p => p.OrderAmount)
}
).ToList();
Apart from being unreadable, and wildly inefficient, this should do the trick:
As a general disclaimer I would urge you to reconsider using linq for formatting data. Hacking up this kind of code is fine if you are tied to using only SQL, but given the chance, there is no shame in just looping over a dataset 🙂