本文介绍了Linq-运行总计和小计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

给出代码后:

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.

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  

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

推荐答案

使用组和总和:

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

这篇关于Linq-运行总计和小计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:20