2015-04-21 29 views
0

下面的代碼輸出「日,月,日,月,日」意外的結果轉換列表陣列

但是我想輸出過去6個月滾動的基礎上 - 誰能幫助?

謝謝!

 DateTime EndDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);//The AddDays function is self explanatory although you can have negative numbers in here 

      DateTime StartDate = EndDate.AddMonths(-1); //When do you want the meetings to start DateTime.Today will return midnight 
      DateTime m = StartDate.AddMonths(-6); 

      List<months> monthstore = new List<months>(); 

      months monthdata = new months(); 

      while (m < StartDate) 
      { 
       monthdata.month = m.ToString("MMMM"); 
       monthstore.Add(monthdata); 

       m = m.AddMonths(1); 
      } 
     var publicationTable = new[] { 
    new[] { monthstore.Select(x=> x.month).ToArray() } 

}; 
+0

我不明白你的問題。 「月」的結構是什麼? –

+0

@SonerGönül真的很重要嗎?只要它不是'struct'(我嚴重懷疑它會是這樣),結果總是相同的 - 單個對象多次添加到列表中。 –

回答

3

months monthdata = new months(); 

進入死循環,你正在改變總是相同的對象。 months是參考類型