2015-05-31 77 views
0

我的LINQ查詢返回一年中每個月的總計,我公開的是匿名類型的十二個屬性(名稱爲一月,二月,三月......)。這使得無法循環十二個值。有沒有辦法改爲返回包含所有12個值的單個索引屬性?從匿名類型返回索引屬性

這是我有:

Dim MyTotals = Aggregate CostRow In AllTargetRows _ 
       Into January = Sum(CostRow.January), 
        February = Sum(CostRow.February), 
        March = Sum(CostRow.March), _ 
        ... 
        Total = Sum(CostRow.Total) 

這就是我想要的實現(編譯器不喜歡這個):

Dim MyTotals = Aggregate CostRow In AllTargetRows _ 
       Into Values = {Sum(CostRow.January), 
           Sum(CostRow.February), 
           Sum(CostRow.March), _ 
           ... 
           }, 
        Total = Sum(CostRow.Total) 

然後我就能夠做到MyTotals.Values(i)樣的東西。

+0

字典呢? – ilans

+0

@ilanS:我猜想會有一個'Dictionary'。問題是,怎麼樣? – dotNET

+0

你究竟做了什麼?「這使得無法遍歷十二個值。」 ? – ilans

回答

0

我首先想到的是使用Aggregate函數填充字典。事情是這樣的:

Dim MyTotals = 
    AllTargetRows 
    .Aggregate(
     new Dictionary<string, int>() 
      { 
       { "January", 0 }, 
       { "February", 0 }, 
       { "March", 0 }, 
       { "April", 0 }, 
       { "May", 0 }, 
       { "June", 0 }, 
       { "July", 0 }, 
       { "August", 0 }, 
       { "September", 0 }, 
       { "October", 0 }, 
       { "November", 0 }, 
       { "December", 0 }, 
       { "Total", 0 } 
      }, 
     (acc, costRow) => 
      { 
       acc["January"] += costRow.January; 
       acc["February"] += costRow.February; 
       acc["March"] += costRow.March; 
       acc["April"] += costRow.April; 
       acc["May"] += costRow.May; 
       acc["June"] += costRow.June; 
       acc["July"] += costRow.July; 
       acc["August"] += costRow.August; 
       acc["September"] += costRow.September; 
       acc["October"] += costRow.October; 
       acc["November"] += costRow.November; 
       acc["December"] += costRow.December; 
       acc["Total"] += costRow.Total; 
       return acc; 
      }); 

這將導致一個字典,其中鍵是一年中的月份(和「總」)和值是每個月的值的總和。

對不起,我錯過了VB.NET標記。 VB.NET不是我的強項,但我認爲這在VB.NET中是等價的。

Dim MyTotals = _ 
    AllTargetRows _ 
    .Aggregate(_ 
     New Dictionary(Of String, Integer) From { _ 
      {"January", 0}, _ 
      {"February", 0}, _ 
      {"March", 0}, _ 
      {"April", 0}, _ 
      {"May", 0}, _ 
      {"June", 0}, _ 
      {"July", 0}, _ 
      {"August", 0}, _ 
      {"September", 0}, _ 
      {"October", 0}, _ 
      {"November", 0}, _ 
      {"December", 0}, _ 
      {"Total", 0} _ 
     }, _ 
     Function(acc, costRow) 
      acc("January") += costRow.January 
      acc("February") += costRow.February 
      acc("March") += costRow.March 
      acc("April") += costRow.April 
      acc("May") += costRow.May 
      acc("June") += costRow.June 
      acc("July") += costRow.July 
      acc("August") += costRow.August 
      acc("September") += costRow.September 
      acc("October") += costRow.October 
      acc("November") += costRow.November 
      acc("December") += costRow.December 
      acc("Total") += costRow.Total 
      Return acc 
     End Function) 

我不知道,如果Aggregate函數的這個重載可以轉換爲查詢語法。一個(可能更簡單的)替代方法是完全放棄LINQ,在運行查詢之前簡單地創建字典,然後循環遍歷每行並以這種方式填充字典。就像這樣:

Dim MyTotals = _ 
    New Dictionary(Of String, Integer) From { _ 
     {"January", 0}, _ 
     {"February", 0}, _ 
     {"March", 0}, _ 
     {"April", 0}, _ 
     {"May", 0}, _ 
     {"June", 0}, _ 
     {"July", 0}, _ 
     {"August", 0}, _ 
     {"September", 0}, _ 
     {"October", 0}, _ 
     {"November", 0}, _ 
     {"December", 0}, _ 
     {"Total", 0}} 

For Each costRow as AllTargetRow in AllTargetRows 
    MyTotals("January") += costRow.January 
    MyTotals("February") += costRow.February 
    MyTotals("March") += costRow.March 
    MyTotals("April") += costRow.April 
    MyTotals("May") += costRow.May 
    MyTotals("June") += costRow.June 
    MyTotals("July") += costRow.July 
    MyTotals("August") += costRow.August 
    MyTotals("September") += costRow.September 
    MyTotals("October") += costRow.October 
    MyTotals("November") += costRow.November 
    MyTotals("December") += costRow.December 
    MyTotals("Total") += costRow.Total 
Next 
+0

這是VB。 NET或C#? – dotNET

+0

你也知道它的查詢語法嗎? – dotNET