簡體   English   中英

使用 C# 的 Linq 嵌套分組?

[英]Linq nested grouping using c#?

我是 LINQ 的新手。 有人可以解釋一下如何更具體地分組嗎?

 public void additems()
 {
     store.Add(new DepartmentalStore() { Department = "Purchasing",
                                         EmployeeID = 3322, 
                                         Product = "Apples", 
                                         Count = 1 });
     store.Add(new DepartmentalStore() { Department = "Purchasing",
                                         EmployeeID = 3322, 
                                         Product = "Oranges", 
                                         Count = 1 });
     store.Add(new DepartmentalStore() { Department = "Purchasing",
                                         EmployeeID = 3311, 
                                         Product = "Oranges", 
                                         Count = 2 });
     store.Add(new DepartmentalStore() { Department = "HR", 
                                         EmployeeID = 1222,  
                                         Product = "Apples",  
                                         Count = 1 });
     store.Add(new DepartmentalStore() { Department = "HR",  
                                         EmployeeID = 1111,  
                                         Product = "Apples",  
                                         Count = 3 });
 }

 var getDep = from row in samples.store
              group row by new { Dep = row.Department, EmpId = row.EmployeeID, 
                                  Prod = row.Product, Count = row.Count } into g
              orderby g.Key.Dep, g.Key.EmpId, g.Key.Prod, g.Key.Count
              select g;
              //group row by row.Department 

 foreach (var it in getDep)
 {
     Console.WriteLine(it.Key);
 }

上面的代碼給出了這樣的結果

{ Dep = HR, EmpId = 1111, Prod = Apples, Count = 3 }
{ Dep = HR, EmpId = 1222, Prod = Apples, Count = 1 }
{ Dep = Purchasing, EmpId = 3311, Prod = Oranges, Count = 2 }
{ Dep = Purchasing, EmpId = 3322, Prod = Apples, Count = 1 }
{ Dep = Purchasing, EmpId = 3322, Prod = Oranges, Count = 1 }

但我希望輸出是這樣的:

 Department: Purchasing
   Employee: 3322
     Product: Apples  Count: 1
     Product: Oranges Count: 2
                      Total  3
   Employee: 3311
     Product: Oranges Count: 2
                      Total: 2
           Purchasing Total: 5

 Department: HR
   Employee: 1222
     Product: Apples  Count: 1
                      Total: 1
   Employee: 1111
     Product: Apples  Count: 3
                      Total: 3
                   HR Total: 4
                Grand Total: 9

有人可以解釋如何像這樣分組,是否可以將一個 LINQ 查詢的輸出傳遞為另一個 LINQ 查詢的輸入?

像這樣的事情應該有效。 我通過定義一個 Department 對象並將它們的列表添加到一個名為 store 的對象來創建一個快速測試。 您需要更新控制台寫行以獲取選項卡格式。

     var getDep = from row in samples.store
        group row by row.Department
        into g
        select new
        {
           DepartmentName = g.Key,
           Employees =
              from emp in g
              group emp by emp.EmployeeID
              into eg
              select new
              {
                 Employee = eg.Key,
                 EmployeeProducts = eg
              }

        };
     foreach (var it in getDep)
     {
        Console.WriteLine(string.Format("Department: {0}", it.DepartmentName));
        foreach (var emp in it.Employees)
        {
           Console.WriteLine(string.Format("Employee: {0}", emp.Employee));
           var totalCount = 0;
           foreach (var product in emp.EmployeeProducts)
           {
              Console.WriteLine(string.Format("Product: {0}  Count: {1}", product.Product, product.Count));
              totalCount += product.Count;
           }

           Console.WriteLine("Total {0}", totalCount);
        }
     }

暫無
暫無

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

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