2012-08-29 134 views
1

我想創建一個二維數組,我越來越困惑。一位同事告訴我,我需要在數組列表的字典中創建一本字典,但他無法堅持幫助我。創建一個二維數組

我已經能夠創建第一個陣列,列出這樣

+ project 1 
+ project 2 
+ project 3 
+ project 4 

,完成此任務的代碼的程序如下─是

var PGList = from x in db.month_mapping 
      where x.PG_SUB_PROGRAM == SP 
      select x; 
      //select x.PG.Distinct().ToArray(); 

var PGRow = PGList.Select(x => new { x.PG }).Distinct().ToArray(); 

這樣把我的垂直的護理數組,現在我需要添加水平數組,以便可以看到每個會計期間花費的總金額。所以最終的輸出看起來像這樣,但當然沒有破折號。

+ program 1-------100---200---300---400---500---600---700---800---900---1000---1100---1200 
+ program 2-------100---200---300---400---500---600---700---800---900---1000---1100---1200 
+ program 3-------100---200---300---400---500---600---700---800---900---1000---1100---1200 
+ program 4-------100---200---300---400---500---600---700---800---900---1000---1100---1200 

我試圖使用foreach循環會計期間,但它不起作用。我想我可能會走在正確的軌道上,我希望能夠提供一些指導,或者至少有一個教程讓我遵循。我已經發布了下面第二個數組中的代碼。我正在使用C#和MVC 3.您可能會注意到它們在字典中沒有字典。如果我的同事是正確的,我該如何做這樣的事情,我看了一下using dictionary as a key in other dictionary這個問題,但我不明白我會如何在這種情況下使用它。

Dictionary<string, double[]> MonthRow = new Dictionary<string, double[]>(); 

double[] PGContent = new double[12]; 

string lastPG = null; 

foreach (var item in PGRow) 
{ 
    if (lastPG != item.PG) 
    { 
     PGContent = new double[12]; 
    } 


    var MonthList = from x in db.Month_Web 
        where x.PG == PG 
        group x by new { x.ACCOUNTING_PERIOD, x.PG, x.Amount } into pggroup 
        select new { accounting_period = pggroup.Key.ACCOUNTING_PERIOD, amount = pggroup.Sum(x => x.Amount) }; 

    foreach (var P in MonthList) 
    { 
     int accounting_period = int.Parse(P.accounting_period) - 1; 
     PAContent[accounting_period] = (double)P.amount; 
     MonthRow[item.PG] = PGContent; 
     lastPG = item.PG; 
    } 

我希望我已經解釋清楚我的問題,請我需要解決這個問題,要經常檢查回隨意問了需要澄清。謝謝你的幫助!

回答

0

我要感謝@Ray鄭和@Dour高拱壩他們的幫助,但我已經找到了另一種方式來完成這個任務,我想後我的代碼以便下一個遇到同樣問題的人可以更快地找出他們的問題。

上面我將我的代碼分成了更多可管理的部分,以儘可能清楚地解釋我的問題,下面的代碼將所有這些部分組合在一起,以便您可以看到大圖。此代碼返回一個包含程序和每個月金額的數組。

public virtual ActionResult getAjaxPGs(string SP = null) 
     { 

      if (SP != null) 
      { 


       var PGList = from x in db.month_mapping 
          where x.PG_SUB_PROGRAM == SP 
          select x; 




       var PGRow = PGList.Select(x => new { x.PG }).Distinct().ToArray(); 

       float[] PGContent = new float[12]; 




       Dictionary<string,float[]> MonthRow = new Dictionary<string, float[]>(); 


       foreach (var item in PGRow) 
       { 

         PGContent = new float[12]; 




        var MonthList = from x in db.month_Web 
            where x.PG == item.PG 
            group x by new { x.ACCOUNTING_PERIOD, x.PG, x.Amount } into pggroup 
            select new { accounting_period = pggroup.Key.ACCOUNTING_PERIOD, amount = pggroup.Sum(x => x.Amount) }; 

        foreach (var mon in MonthList) 
        { 
         int accounting_period = int.Parse(mon.accounting_period) - 1; 
         PGContent[accounting_period] = (float)mon.amount/1000000; 
        } 


        MonthRow[item.PG] = PGContent; 

        } 





       return Json(MonthRow, JsonRequestBehavior.AllowGet); 

      } 


      return View(); 
     } 

此代碼爲我工作的偉大的,因爲我是從一個LINQ拉至SQL查詢,而不是直接將數據添加到代碼中。我的問題源於主要將數據拉到foreach循環之外,所以它只從SQL中取出1條數據,而不是全部12個月。我希望這可以幫助那些試圖從SQL數據源中將數據提取到多維數組中的人。

1

不要嘗試使用匿名類型或LINQ投影來創建新的數據類型,特別是如果您是初學者,您只會感到困惑。如果你想要一個專門的數據類型,請定義一個;例如:

public class Account 
{ 
    public string Name { get; private set; } 
    public decimal[] MonthAmount { get; private set; } 

    readonly int maxMonths = 12; 

    public Account(string name, ICollection<decimal> monthAmounts) 
    { 
     if (name == null) 
      throw new ArgumentNullException("name"); 

     if (monthAmounts == null) 
      throw new ArgumentNullException("monthAmounts"); 

     if (monthAmounts.Count > maxMonths) 
      throw new ArgumentOutOfRangeException(string.Format(" monthAmounts must be <= {0}", maxMonths)); 

     this.Name = name; 

     this.MonthAmount = new decimal[maxMonths]; 
     int i = 0; 
     foreach (decimal d in monthAmounts) 
     { 
      this.MonthAmount[i] = d; 
      i++; 
     } 
    } 
} 

這種直接的使用情況,您不必將它們轉換爲數組,字典,列表,或其他任何東西:

var accountPeriods = new List<Account>(); 
accountPeriods.Add(new Account("program-1", new decimal[] { 1, 2, 3, 4 })); 

您可以使用LINQ或任何查詢或改變你的新類型的實例:

foreach (Account a in accountPeriods) 
    foreach (decimal d in a.MonthAmount) 
     DoSomethingWith(d); 

這應該足以讓你開始。

+0

非常感謝您的回覆,很抱歉,我一直無法儘快回覆您,但我正試着理解您給我的代碼。我遇到了foreach的問題,因爲我正在從Linq到SQL查詢獲取數據,所以我認爲我必須將它們轉換爲字典。 – Goldentp

+0

@Goldentp,「有問題」是什麼意思?您可以將類似'Account'的類轉換爲LINQ。請發佈'month_mapping'的定義;如果它來自LINQ,您可以從調試器獲取定義。 –

1

希望這有助於。

// sample data 
var data = new Dictionary<string, List<int>>(); 
data.Add("program-1", new List<int>() { 100, 110, 130 }); 
data.Add("program-2", new List<int>() { 200, 210, 230 }); 
data.Add("brogram-3", new List<int>() { 300, 310, 330 }); 

// query data 
var newData = (from x in data 
       where x.Key.Contains("pro") 
       select x).ToDictionary(v => v.Key, v=>v.Value); 

// display selected data 
foreach (var kv in newData) 
{ 
    Console.Write(kv.Key); 
    foreach (var val in kv.Value) 
    { 
     Console.Write(" "); 
     Console.Write(val.ToString()); 
    } 
    Console.WriteLine(); 
} 

輸出爲:

program-1 100 110 130 
program-2 200 210 230