2015-08-28 59 views
-1

我是LINQ的新手。有人可以解释如何更具体地分组吗?Linq使用c#嵌套分组?

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查询的输入?

+7

这种“显示”(抑制重复标题,添加小计和总计)比Linq更适合显示层。 –

+0

“是否可以将来自一个LINQ查询的输出传递给另一个LINQ查询的输入”当然,但您必须更具体(最好在单独的问题中) –

回答

0

像这样的东西应该工作。我通过定义一个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); 
     } 
    }