小编Ton*_*ick的帖子

C#循环遍历List <dictionary <string,string >>以填充DataTable

我需要遍历一个词典列表

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)

c# datatable dictionary loops list

4
推荐指数
1
解决办法
2万
查看次数

无法访问要在HandleRequest中使用的EventArgs e值

我只是在了解活动,代表和订阅者.我花了最近两天的时间来研究和包裹我的大脑.我无法访问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)

c# events eventargs

1
推荐指数
1
解决办法
1415
查看次数

标签 统计

c# ×2

datatable ×1

dictionary ×1

eventargs ×1

events ×1

list ×1

loops ×1