tags:

views:

497

answers:

2

I tried out some code to get the "running total" and "subtotal" ,but that did yield successful result.

When the code is given:

var salesPeople =
new SalesPerson[] 
{ 
new SalesPerson{Name="Ricky", RegionCode="R001",SalesAmount=100,
SalesDate=Convert.ToDateTime("01/Jan/2009")},

new SalesPerson{Name="Mark", RegionCode="R001",SalesAmount=200,
SalesDate=Convert.ToDateTime("02/Jan/2009")},

new SalesPerson{Name="Jon", RegionCode="R001",SalesAmount=400,
SalesDate=Convert.ToDateTime("10/Jan/2009")},

new SalesPerson{Name="Ricky", RegionCode="R001",SalesAmount=100, 
SalesDate=Convert.ToDateTime("05/Jan/2009")},

new SalesPerson{Name="Mark", RegionCode="R001",SalesAmount=200,
SalesDate=Convert.ToDateTime("07/Jan/2009")},

new SalesPerson{Name="Jon", RegionCode="R001",SalesAmount=250,
SalesDate=Convert.ToDateTime("11/Jan/2009")},

new SalesPerson{Name="Ricky", RegionCode="R002",SalesAmount=50,
SalesDate=Convert.ToDateTime("01/feb/2009")},

new SalesPerson{Name="Mark", RegionCode="R002",SalesAmount=120,
SalesDate=Convert.ToDateTime("02/feb/2009")},

new SalesPerson{Name="Peter", RegionCode="R002",SalesAmount=30,
SalesDate=Convert.ToDateTime("10/feb/2009")},

new SalesPerson{Name="Ricky", RegionCode="R002",SalesAmount=400, 
SalesDate=Convert.ToDateTime("05/feb/2009")},

new SalesPerson{Name="Mark", RegionCode="R002",SalesAmount=70,
SalesDate=Convert.ToDateTime("07/feb/2009")},

new SalesPerson{Name="Peter", RegionCode="R002",SalesAmount=60,
SalesDate=Convert.ToDateTime("11/feb/2009")}

};

How can i find the Total,SubTotal,RunningTotal similiar to the following one.

       (1) Running Total Based on Region and Month

                            Sales History
        -------------------------------------------------------------
        Region Code     Name       MonthyRunningTotal      SalesDate          
                                                           (By Month)

        -------------------------------------------------------------

         R001           Ricky        100                   01/Jan/2009
         R001           Ricky        200                   05/Jan/2009


         R002           Ricky         50                   01/feb/2009
         R002           Ricky        450                   05/feb/2009  

         R001           Mark         200                   02/Jan/2009
         R001           Mark         400                   07/Jan/2009


         R002           Mark        120                    02/feb/2009
         R002           Mark        190                    07/feb/2009  

         R001           Jon         400                    10/Jan/2009
         R001           Jon         650                    11/Jan/2009  

         R002           Peter        30                    10/feb/2009
         R002           Peter        90                    11/feb/2009

and

             (2)  Total and Subtotal based on Region and Month

                  Sales History (Based on Region and Month)
        -------------------------------------------------------------
        Region Code     Name       MonthyRunningTotal      SalesDate          
                                                           (By Month)    
        -------------------------------------------------------------

         R001           Ricky        100                   01/Jan/2009
         R001           Ricky        100                   05/Jan/2009

                        Total        ----
                                     200 
                                     ----  

         R002           Ricky         50                   01/feb/2009
         R002           Ricky        400                   05/feb/2009  

                         Total       ----
                                     450
                                     ----    

         R001           Mark         200                   02/Jan/2009
         R001           Mark         200                   07/Jan/2009

                        Total       -----
                                     400
                                    -----  

         R002           Mark        120                    02/feb/2009
         R002           Mark         70                    07/feb/2009  

                        Total       ----
                                    190
                                    ----  

         R001           Jon         400                    10/Jan/2009
         R001           Jon         250                    11/Jan/2009  
                                    ----
                                    650
                                    ----

         R002           Peter        30                    10/feb/2009
         R002           Peter        60                    11/feb/2009  
                                    -----
                                     90
                                    -----
A: 

use Group and Sum:

from s in SalesPeople 
group s by s.RegionCode into g 
select new {Category=g.Key, Sum = g.Group.Sum(p => p.SalesAmount)}

See also http://www.develop-one.net/blog/2007/11/11/LINQSumAndGroupBy.aspx

Manu
this does not answer the OP's question
Tion
Doesn't answer the question. See http://stackoverflow.com/questions/1834753/linq-to-sql-and-a-running-total-on-ordered-results
Patricker
+4  A: 

I found this question while looking for an answer to the running total problem. I was disappointed that the answer doesn't seem to address that issue, so I asked my own question and got a great answer. Hope this helps anyone else trying to do running totals.

Ecyrb