2011-11-01 44 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