DataTable to Dictionary <string,Dictionary <string,string >>

Pra*_*een 1 c# linq dataset

我的DataTable包含17列,其中我正在检索3列.对于Instance,我们将这3列视为colA,colB,colC.我的要求是,结果应该是格式

Dictionary<string, Dictionary<string,string>> ( Dictionary<colA,Dictionary<colB,colC>> )
Run Code Online (Sandbox Code Playgroud)

使用LINQ会更好......!

Dictionary<string, Dictionary<string, string>> roles = TA_Roles.GetRoleByUsername(Username)
    .Select(col => new { col.RoleID, col.Rolecode, col.Rolename }) 
    //Please continue from here..!
Run Code Online (Sandbox Code Playgroud)

Mt.*_*ers 5

如果colA是唯一的,这应该有效:

Dictionary<string, Dictionary<string, string>> result = table.AsEnumerable().ToDictionary(row => row["colA"].ToString(),
                                                                                          row => new string[] { "colB", "colC" }.ToDictionary(col => col, col => row[col].ToString()));
Run Code Online (Sandbox Code Playgroud)