2011-11-01 42 views
10

我是新來的ASP .NET與C#現在我需要解決的一個問題PHP陣列核心價值用C#

我可以創建這樣

$arr[] = array('product_id' => 12, 'process_id' => 23, 'note' => 'This is Note'); 

//Example 
Array 
(
    [0] => Array 
     (
      [product_id] => 12 
      [process_id] => 23 
      [note] => This is Note 
     ) 

    [1] => Array 
     (
      [product_id] => 5 
      [process_id] => 19 
      [note] => Hello 
     ) 

    [2] => Array 
     (
      [product_id] => 8 
      [process_id] => 17 
      [note] => How to Solve this Issue 
     ) 

) 

我想一個數組使用C#在ASP.NET中創建相同的數組結構。

請幫我解決這個問題。提前致謝。

回答

20

使用Dictionary<TKey, TValue>快速查找基於鍵(您的字符串)的值(您的對象)。

var dictionary = new Dictionary<string, object>(); 
dictionary.Add("product_id", 12); 
// etc. 

object productId = dictionary["product_id"]; 

爲了簡化Add操作,您可以使用集合初始化語法如

var dictionary = new Dictionary<string, int> { { "product_id", 12 }, { "process_id", 23 }, /* etc */ }; 

編輯

與您的更新,我會繼續前進,定義一個適當的鍵入以封裝您的數據

class Foo 
{ 
    public int ProductId { get; set; } 
    public int ProcessId { get; set; } 
    public string Note { get; set; } 
} 

然後創建該類型的數組或列表。

var list = new List<Foo> 
      { 
       new Foo { ProductId = 1, ProcessId = 2, Note = "Hello" }, 
       new Foo { ProductId = 3, ProcessId = 4, Note = "World" }, 
       /* etc */ 
      }; 

然後,你還要強類型的對象,一個列表中,您可以遍歷,綁定到控件等

var firstFoo = list[0]; 
someLabel.Text = firstFoo.ProductId.ToString(); 
anotherLabel.Text = firstFoo.Note; 
+0

感謝您的快速回復, 陣列 ( [0] =>數組 ( [PRODUCT_ID] => 12 [process_id] => 23 [注] =>這是註釋 ) [1] =>數組 ( [PRODUCT_ID] => 5 [PROCESS_ID] => 19 [注] =>你好 ) [2] =>數組 ( [PRODUCT_ID] => 8 [PROCESS_ID] => 17 [注] =>如何如果你想這樣做,我會簡單地定義一個類來封裝解決這個問題 ) ) 我想做的事情是這樣的... – rkaartikeyan

+0

你的數據,只是有一個數組或該類的實例列表。喬恩的回答可能暗示了一種匿名類型,這也是一種選擇。 –

+0

肯定先生,這對我很有幫助。 – rkaartikeyan

4

如果你正在尋找從string映射到object

Dictionary<string, object> map = new Dictionary<string, object> { 
    { "product_id", 12 }, 
    { "process_id", 23 }, 
    { "note", "This is Note" } 
}; 

或者,也許你想一個匿名類,如果這僅僅是一個周圍傳遞數據的方式:

var values = new { 
    ProductId = 12, 
    ProcessId = 23, 
    Note = "This is Note" 
}; 

這實際上取決於你想達到的目標 - 更大的圖片。

編輯:如果你有多個值相同的「鍵」,我可能會爲此創建一個特定的類型 - 目前還不清楚這是什麼類型的實體代表,但你應該創建一個類來建模,並根據需要爲其添加適當的行爲。

+0

感謝您給予快速回復,我修改了問題,請看看先生。實際上,我將product_id,process_id,note存儲在每個表單文章的數組中。最後,我將操縱數組並存儲到數據庫中。 – rkaartikeyan

+1

@ user707852:這並不能真正改變我的答案 - 您可以將我的答案應用於這兩種情況。說實話,我懷疑你最好創建一個類型來封裝這些值... –

+0

感謝您的時間 – rkaartikeyan

1

關聯數組可以使用Dictionary在C#中表示。 它的Enumerator.Current會返回一個keyValuePair。

所以你的陣列想

var associativeArray = new Dictionary<string, string>(){ {"product_id", "12"}, {"process_id"," 23", {"note","This is Note"}}; 
+0

感謝您的時間 – rkaartikeyan

1

嘗試此

System.Collections.Generic.Dictionary<string, object>[] map = new System.Collections.Generic.Dictionary<string, object>[10]; 

map[0] = new System.Collections.Generic.Dictionary<string,object>(); 
map[0].Add("product_id", 12); 
map[0].Add("process_id", 23); 
map[0].Add("note", "This is Note"); 

map[1] = new System.Collections.Generic.Dictionary<string,object>(); 
map[1].Add("product_id", 5); 
map[1].Add("process_id", 19); 
map[1].Add("note", "Hello"); 
+0

感謝您的時間 – rkaartikeyan

+0

我覺得'名單'比一個數組在這裏。 – svick

+0

Oups是與編輯的問題,相同的結構,它會使更多的意義也使用上述答案,它更加優雅。 – Khan