C# 数据表到字典<;字符串、字典<;字符串,字符串>&燃气轮机;

C# 数据表到字典<;字符串、字典<;字符串,字符串>&燃气轮机;,c#,linq,dataset,C#,Linq,Dataset,我的DataTable包含17列,其中我正在检索3列。例如,我们认为这3个列为可乐,COLB,COLC。我的要求是,结果的格式应该是 Dictionary<string, Dictionary<string,string>> ( Dictionary<colA,Dictionary<colB,colC>> ) 字典(字典) 使用LINQ会更好 Dictionary<string, Dictionary<string, string&

我的DataTable包含17列,其中我正在检索3列。例如,我们认为这3个列为可乐,COLB,COLC。我的要求是,结果的格式应该是

Dictionary<string, Dictionary<string,string>> ( Dictionary<colA,Dictionary<colB,colC>> )
字典(字典)
使用LINQ会更好

Dictionary<string, Dictionary<string, string>> roles = TA_Roles.GetRoleByUsername(Username)
    .Select(col => new { col.RoleID, col.Rolecode, col.Rolename }) 
    //Please continue from here..!
Dictionary roles=TA_roles.GetRoleByUsername(用户名)
.Select(col=>new{col.RoleID,col.Rolecode,col.Rolename})
//请从这里继续。。!
似乎
字典在这里不太正确,因为
col1
不能确保数据的唯一性,您可以使用
列表

 var result = table.AsEnumerable().Select(row => 
                Tuple.Create<string, string, string>(row.Field<string>("col1"), 
                                                     row.Field<string>("col2"), 
                                                     row.Field<string>("col3")));
var result=table.AsEnumerable().Select(行=>
Tuple.Create(row.Field(“col1”),
行字段(“col2”),
行字段(“col3”);
似乎
字典在这里不太正确,因为
col1
不能确保数据的唯一性,您可以使用
列表

 var result = table.AsEnumerable().Select(row => 
                Tuple.Create<string, string, string>(row.Field<string>("col1"), 
                                                     row.Field<string>("col2"), 
                                                     row.Field<string>("col3")));
var result=table.AsEnumerable().Select(行=>
Tuple.Create(row.Field(“col1”),
行字段(“col2”),
行字段(“col3”);

根据col2/col3组合是否唯一,我有两种解决方案

class Role
{
    public string RoleID { get; set; }
    public string Rolecode { get; set; }
    public string Rolename { get; set; }
}

IEnumerable<Role> source = ...;

Dictionary<string, Dictionary<string, List<string>>> result = source
    .GroupBy(r => r.RoleID)
    .ToDictionary(g => g.Key,
         g => g.GroupBy(r2 => r2.Rolecode)
        .ToDictionary(g2 => g2.Key,
            g2 => g2.Select(r3 => r3.Rolename).ToList())
    );

// Rolecode unique
Dictionary<string, Dictionary<string, string>> result2 = source
    .GroupBy(r => r.RoleID)
    .ToDictionary(g => g.Key,
        g => g.ToDictionary(r2 => r2.Rolecode, r2 => r2.Rolename)
    );
类角色
{
公共字符串RoleID{get;set;}
公共字符串角色代码{get;set;}
公共字符串Rolename{get;set;}
}
IEnumerable源=。。。;
字典结果=源
.GroupBy(r=>r.RoleID)
.ToDictionary(g=>g.Key,
g=>g.GroupBy(r2=>r2.Rolecode)
.ToDictionary(g2=>g2.Key,
g2=>g2.Select(r3=>r3.Rolename.ToList())
);
//角色代码独特
Dictionary result2=源
.GroupBy(r=>r.RoleID)
.ToDictionary(g=>g.Key,
g=>g.ToDictionary(r2=>r2.Rolecode,r2=>r2.Rolename)
);
但如果这三列的所有组合都是唯一的,那么整个事情就毫无意义了。然而,创建两本词典是有意义的

Dictionary<string, Role> rolesByID = source.ToDictionary(r => r.RoleID);
Dictionary<string, Role> rolesByCode = source.ToDictionary(r => r.Rolecode);
Dictionary rolesByID=source.ToDictionary(r=>r.RoleID);
Dictionary rolesByCode=source.ToDictionary(r=>r.Rolecode);

根据col2/col3组合是否唯一,我有两种解决方案

class Role
{
    public string RoleID { get; set; }
    public string Rolecode { get; set; }
    public string Rolename { get; set; }
}

IEnumerable<Role> source = ...;

Dictionary<string, Dictionary<string, List<string>>> result = source
    .GroupBy(r => r.RoleID)
    .ToDictionary(g => g.Key,
         g => g.GroupBy(r2 => r2.Rolecode)
        .ToDictionary(g2 => g2.Key,
            g2 => g2.Select(r3 => r3.Rolename).ToList())
    );

// Rolecode unique
Dictionary<string, Dictionary<string, string>> result2 = source
    .GroupBy(r => r.RoleID)
    .ToDictionary(g => g.Key,
        g => g.ToDictionary(r2 => r2.Rolecode, r2 => r2.Rolename)
    );
类角色
{
公共字符串RoleID{get;set;}
公共字符串角色代码{get;set;}
公共字符串Rolename{get;set;}
}
IEnumerable源=。。。;
字典结果=源
.GroupBy(r=>r.RoleID)
.ToDictionary(g=>g.Key,
g=>g.GroupBy(r2=>r2.Rolecode)
.ToDictionary(g2=>g2.Key,
g2=>g2.Select(r3=>r3.Rolename.ToList())
);
//角色代码独特
Dictionary result2=源
.GroupBy(r=>r.RoleID)
.ToDictionary(g=>g.Key,
g=>g.ToDictionary(r2=>r2.Rolecode,r2=>r2.Rolename)
);
但如果这三列的所有组合都是唯一的,那么整个事情就毫无意义了。然而,创建两本词典是有意义的

Dictionary<string, Role> rolesByID = source.ToDictionary(r => r.RoleID);
Dictionary<string, Role> rolesByCode = source.ToDictionary(r => r.Rolecode);
Dictionary rolesByID=source.ToDictionary(r=>r.RoleID);
Dictionary rolesByCode=source.ToDictionary(r=>r.Rolecode);

如果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()));
Dictionary result=table.AsEnumerable().ToDictionary(行=>row[“colA”].ToString(),
row=>newstring[]{“colB”,“colC”}.ToDictionary(col=>col,col=>row[col].ToString());

如果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()));
Dictionary result=table.AsEnumerable().ToDictionary(行=>row[“colA”].ToString(),
row=>newstring[]{“colB”,“colC”}.ToDictionary(col=>col,col=>row[col].ToString());

但是colA是表中的主键,我会查字典colA@Praveen:一个数据colA在colB中只有一个数据,在colC中只有一个数据,不应该是Dictionary?但是colA是表中的主键,我将通过Dictionary查看colA@Praveen:一个数据colA只有一个colB中的数据和一个colC中的数据,不应该是字典吗?