C# 将字典作为元素传递给另一个字典
我需要运行for循环,将数据从excel增量加载到字典,然后将其传递到另一个字典。我正在为此使用以下代码:C# 将字典作为元素传递给另一个字典,c#,asp.net,.net,dictionary,C#,Asp.net,.net,Dictionary,我需要运行for循环,将数据从excel增量加载到字典,然后将其传递到另一个字典。我正在为此使用以下代码: var contr = new Dictionary<string, Dictionary<string, object>>(); contr.Add("Contributor" + (j + 1), contributor_dictionary); item.Add(contributor_list + (j + 1), contr); 当我将其添加到项目字典时
var contr = new Dictionary<string, Dictionary<string, object>>();
contr.Add("Contributor" + (j + 1), contributor_dictionary);
item.Add(contributor_list + (j + 1), contr);
当我将其添加到项目字典时,它将输出为:
contributor_list1:{
contributor1:{
name: abc,
title: xyz }
contributor_list2:{
contributor2:{
name: def,
title: mno } and so on..
而我希望它的格式是:
contributor:{
name: abc,
title: xyz }
contributor_list:{
contributor1:{
name: abc,
title: xyz },
contributor2:{
name: def,
title: mno }
}
如何实现这一点?我认为每次循环时,您都在向项目字典添加贡献者。首先创建contr dictionary集合,然后将其添加到项目中。 不需要在条目dict键中添加索引。 只需写下:
添加项目(投稿人名单,合同);
如果contributor\u list是常量/固定字符串,则项字典似乎是多余的,但如果它发生更改,则其为Ok。假设您以字典的形式从Excel接收数据,则需要以下内容:
public class Contributor
{
int ContributorID {get; set;} // optional
string name {get; set;}
string title {get; set;}
}
然后将Excel字典解压到Contributor
对象中,如果需要键(上面没有使用键),请将该对象添加到字典中
如果您不需要密钥,那么只需使用
贡献者列表
我也遇到过类似的情况,我就是这样处理的。
因为您知道数据的格式,所以可以使用这样的结构
public struct ExcellData
{
public string Name{ get; set; }
public string Title{ get; set; }
}
然后,您现在可以有一个字典来保存每个excel数据,如下所示
public class MyDictionary : List<ExcellData>
{
public void Add(string key, string Name, string Title)
{
ExcellData val = new ExcellData();
val.Name= Name;
val.Title= Title;
this.Add(val);
}
}
public struct ExcellCells
{
public string Someoption { get; set; }
public string SomeOptionType { get; set; }
public string[] cellList { get; set; }
}
public class MyDictionary : List<ExcellCells>
{
public void Add(string key, string option, string[] xcell)
{
ExcellCells val = new ExcellCells();
val.SomeOptionType = key;
val.Someoption = option;
val.cellList = xcell;
this.Add(val);
}
}
假设excel数据在一行上有多个值,那么代码可能如下所示
public class MyDictionary : List<ExcellData>
{
public void Add(string key, string Name, string Title)
{
ExcellData val = new ExcellData();
val.Name= Name;
val.Title= Title;
this.Add(val);
}
}
public struct ExcellCells
{
public string Someoption { get; set; }
public string SomeOptionType { get; set; }
public string[] cellList { get; set; }
}
public class MyDictionary : List<ExcellCells>
{
public void Add(string key, string option, string[] xcell)
{
ExcellCells val = new ExcellCells();
val.SomeOptionType = key;
val.Someoption = option;
val.cellList = xcell;
this.Add(val);
}
}
public struct excells
{
公共字符串Someoption{get;set;}
公共字符串SomeOptionType{get;set;}
公共字符串[]单元格列表{get;set;}
}
公共类MyDictionary:列表
{
公共void添加(字符串键、字符串选项、字符串[]xcell)
{
ExcellCells val=新的ExcellCells();
val.SomeOptionType=key;
val.Someoption=选项;
val.cellList=xcell;
添加(val);
}
}
希望这有帮助不,你不想那样。不要在变量名中添加索引。嗨@JohnWu。我不明白你的意思如果关键的区别只是一个索引号,那么为什么要使用字典而不仅仅是列表呢还有,为什么要查字典?您是否计划持有与“贡献者”结构不同的东西?还有:什么是
Contributor\u dictionary
和item
?你是说“贡献者列表”而不是“贡献者列表”?或者contributor\u list
是字符串变量/常量吗?--由于您在其中添加了contr
,item
将是一个字典,请创建一个表示您的数据的类或结构-这将节省时间并使您和您的大学的生活更轻松。这显然比OP方法好,但我不会继承列表,而是将其封装为专用类型。