ASP .NET中使用C#实现数组键值对

16

我是初学者,正在学习C#的asp.net。现在我需要解决一个问题。

在PHP中,我可以像这样创建一个数组:

$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中创建与此相同的数组结构。

请帮我解决这个问题。

4个回答

40

使用 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] => 数组 ( [product_id] => 12 [process_id] => 23 [note] => 这是注释 )[1] => 数组 ( [product_id] => 5 [process_id] => 19 [note] => 你好 ) [2] => 数组 ( [product_id] => 8 [process_id] => 17 [note] => 如何解决这个问题 ))我想做类似于这样的事情... - rkaartikeyan
如果你想这样做,我会建议你定义一个类来封装你的数据,并且只需要有一个该类实例的数组或列表。Jon的回答提出了使用匿名类型的可能性,这也是一种选择。 - Anthony Pegram
当然,先生。这对我来说将是非常有帮助的。 - rkaartikeyan

5
如果您正在寻找从`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"
};

这真的取决于你想要实现什么 - 更大的图景。

编辑:如果你有多个值使用相同的“键”,我可能会为此创建一个特定类型 - 不清楚这是表示什么实体,但应该创建一个类来对其进行建模,并根据需要添加适当的行为。


感谢您快速回复,我修改了问题,请您查看一下。实际上,我将每个表单提交的product_id、process_id和注释存储到数组中。最后,我将操作这个数组并将其存储到数据库中。 - rkaartikeyan
1
@user707852:那并不会改变我的答案 - 你可以将我的答案应用于任何一种情况。但说实话,我认为你最好创建一个类型来封装这些值... - Jon Skeet

2

试试这个

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");

我认为在这里使用List<T>比使用数组更合理。 - svick
哦,是的,使用编辑后的问题,相同的结构,使用上面的答案会更有意义,而且更加优雅。 - Khan

1

在C#中,可以使用Dictionary表示关联数组。它的Enumerator.Current会返回一个keyValuePair。

因此,你的数组将会是这样的

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接