Linq-运行总计和子总计

时间:2009-10-16 08:28:48

标签: c# linq

我尝试了一些代码来获取“运行总计”和“小计”,但这确实产生了成功的结果。

当给出代码时:

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")}

};

如何找到与以下类似的Total,SubTotal,RunningTotal。

       (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  

             (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
                                    ----- 

2 个答案:

答案 0 :(得分:15)

我在寻找运行总问题的答案时发现了这个问题。我很失望答案似乎没有解决这个问题,所以我问自己question并得到了一个很好的答案。希望这有助于其他任何人尝试运行总计。

答案 1 :(得分:-1)

使用Group和Sum:

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

另见http://www.develop-one.net/blog/2007/11/11/LINQSumAndGroupBy.aspx