我需要遍历一个词典列表
List<Dictionary<string,string>>
Run Code Online (Sandbox Code Playgroud)
填充DataTable.列表中的每个字典都有一个Key,它需要是列名,而Value是该列中的值.该列表包含225个词典(表格中有225行).
List<Dictionary<string, string>> myList =
JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(jsonRep);
DataTable dt = new DataTable();
//loop through list, loop through dictionaries, add keys as columns,
//values as rows.
Run Code Online (Sandbox Code Playgroud)
到目前为止,我一直在努力..
//get max columns
int columns = myList[0].Count; <--gives me 13
//add columns
for (int i = 0; i < columns; i++)
dt.Columns.Add(string myList[i].Keys); <--somehow get to the key in dict to add as column names
//add rows
foreach (var x in myList)
{
dt.Rows.Add(x); <--not working
}
jsonReprValue = dt; <--save new …Run Code Online (Sandbox Code Playgroud) 我只是在了解活动,代表和订阅者.我花了最近两天的时间来研究和包裹我的大脑.我无法访问EventArgs e值中传递的信息.我有一个想要打开的已保存项目.必要形式的状态被反序列化为字典.触发一个循环,引发UnpackRequest传递键/值.
ProjectManager.cs文件:
public delegate void EventHandler<TArgs>(object sender, TArgs args) where TArgs : EventArgs;
public event EventHandler<UnpackEventArgs> UnpackRequest;
Run Code Online (Sandbox Code Playgroud)
然后再往下走:
ProjectManager.cs文件:
//Raise a UnpackEvent //took out virtual
protected void RaiseUnpackRequest(string key, object value)
{
if (UnpackRequest != null) //something has been added to the list?
{
UnpackEventArgs e = new UnpackEventArgs(key, value);
UnpackRequest(this, e);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在open方法中,在使用每个表单的状态填充字典之后:
ProjectManager.cs文件:
foreach (var pair in dictPackState) {
string _key = pair.Key;
dictUnpackedState[_key] = dictPackState[_key];
RaiseUnpackRequest(pair.Key, pair.Value); //raises the event.
}
Run Code Online (Sandbox Code Playgroud)
我有一个活动课:
public class UnpackEventArgs …Run Code Online (Sandbox Code Playgroud)