-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查詢的輸入?
這種「顯示」(抑制重複標題,添加小計和總計)比Linq更適合顯示層。 –
「是否可以將來自一個LINQ查詢的輸出傳遞給另一個LINQ查詢的輸入」當然,但您必須更具體(最好在單獨的問題中) –