C# 用C实现ASP.NET中的数组键值#
我不熟悉asp.net和C。现在我需要一个问题的解决方案 在PHP中,我可以创建如下数组:C# 用C实现ASP.NET中的数组键值#,c#,asp.net,arrays,key,C#,Asp.net,Arrays,Key,我不熟悉asp.net和C。现在我需要一个问题的解决方案 在PHP中,我可以创建如下数组: $arr[] = array('product_id' => 12, 'process_id' => 23, 'note' => 'This is Note'); //Example Array ( [0] => Array ( [product_id] => 12 [process_id] =>
$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中创建相同的数组结构
请帮助我解决此问题。使用字典
根据键(字符串)快速查找值(对象)
编辑 有了您的更新,我将继续定义一个合适的类型来封装您的数据
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 */
};
如果要查找从
字符串
到对象
的映射:
Dictionary<string, object> map = new Dictionary<string, object> {
{ "product_id", 12 },
{ "process_id", 23 },
{ "note", "This is Note" }
};
这真的取决于你想要实现什么——更大的目标
编辑:如果你对多个值有相同的“键”,我可能会为此创建一个特定的类型-不清楚这是要表示什么类型的实体,但你应该创建一个类来对其进行建模,并根据需要向其添加适当的行为。关联数组可以用C#使用字典表示。 其枚举数.Current将返回keyValuePair 因此,您的阵列将
var associativeArray = new Dictionary<string, string>(){ {"product_id", "12"}, {"process_id"," 23", {"note","This is Note"}};
var associateArray=new Dictionary(){{“product_id”,“12”},{“process_id”,“23”,{“note”,“This is note”};
试试这个
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");
System.Collections.Generic.Dictionary[]map=new System.Collections.Generic.Dictionary[10];
map[0]=新的System.Collections.Generic.Dictionary();
映射[0]。添加(“产品标识”,12);
映射[0]。添加(“进程id”,23);
映射[0]。添加(“注意”,“这是注意”);
map[1]=新的System.Collections.Generic.Dictionary();
映射[1]。添加(“产品标识”,5);
映射[1]。添加(“进程id”,19);
地图[1]。添加(“注意”,“你好”);
感谢您的快速回复,数组([0]=>Array([product\u id]=>12[process\u id]=>23[note]=>这是note)[1]=>Array([product\u id]=>5[process\u id]=>19[note]=>您好)[2]=>Array([产品标识]=>8[流程标识]=>17[注意]=>如何解决此问题)我想这样做…如果你想这样做,我只需要定义一个类来封装你的数据,并有一个数组或该类的实例列表。Jon的回答可能建议使用匿名类型,这也是一个选项。当然,先生,这将对我有很大帮助。非常感谢…非常感谢感谢你给我一个快速的答复,我修改了我问了这个问题,先生,请看一看。实际上,我正在为每个表单帖子将产品id、过程id、注释存储到数组中。最后,我将操作数组并存储到数据库中。@user707852:这并没有真正改变我的答案-您可以将我的答案应用于任何一种情况。不过,老实说,我怀疑您最好创建一个类型要封装这些值…我认为列表
比这里的数组更有意义。是的,对于经过编辑的问题,同样的结构,使用上面的答案也会更有意义,而且更优雅。
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"
};
var associativeArray = new Dictionary<string, string>(){ {"product_id", "12"}, {"process_id"," 23", {"note","This is Note"}};
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");