簡體   English   中英

Linq-運行總計和小計

[英]Linq- Running Total and Sub Total

我嘗試了一些代碼來獲取“運行總計”和“小計”,但這確實產生了成功的結果。

當給出代碼時:

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

我在尋找運行總計問題的答案時發現了這個問題。 我對答案似乎無法解決該問題感到失望,因此我提出了自己的問題 ,得到了一個很好的答案。 希望這對嘗試進行總計的其他人有所幫助。

使用組和總和:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM