【问题标题】:Working with Dictionary<int, Dictionary<string,int> in asp.net在 asp.net 中使用 Dictionary<int, Dictionary<string,int>
【发布时间】:2014-03-22 04:31:06
【问题描述】:

我需要从上面的嵌套字典中获取字符串值。怎么做。 我有从数据表 [3 列] 到字典的 keypairvalue,像这样

management = dt.AsEnumerable()
               .GroupBy(m => m.Field<int>("ID"))
               .ToDictionary(
                   a => a.Key,
                   a => a.GroupBy(c => c.Field<string>("Name"))
                         .ToDictionary(
                             d => d.Key,
                             d => d.First().Field<int>("Parent")));

string temp = 管理???

编辑:让我详细说明一下。

我有一个这样的管理表

ID    Name           Parent
1   Manager             0
2   Accountant          0
3   Assistant Manager   1
4   Branch Manager      1
5   Employee1           3
6   Employee3           3
7   Employee2           4
8   Accountant1         2
9   Accountant2         2

父级表示我的asp页面中这样的树视图结构

*Manager-->Assistant manager-->Employee1
        |                   |->Employee3
        |
        |-->Branch Manager---->Employee2
*Accountant-->Accountant1
           |->Accountant2    

我只需要使用字典将我的数据表显示到树视图 所以我做了上面的代码来将数据表绑定到字典。 现在尝试将字典绑定到树视图 是清楚的伙伴吗 PS:我是asp新手

【问题讨论】:

  • 你需要提取什么样的值?只是"Name"s?
  • 我还需要“id”和“parent”。如果你告诉我如何从“name”中获取第一个字符串值和某些字符串值,那么我会管理的。字典有 9 行 btw

标签: c# asp.net dictionary


【解决方案1】:

您可以像这样查找嵌套字典值

var dt1 = new Dictionary<string, int> { { "Test1", 1 }, { "Test2", 1 }, { "Test3", 1 } };
var dt2 = new Dictionary<string, int> { { "Test11", 101 }, { "Test22", 201 }, { "Test33", 301 } };

var dt3 = new Dictionary<int, Dictionary<string, int>> { { 11, dt1 }, { 22, dt2 } };

foreach (var kvp in dt3)
{
    var innerDict = kvp.Value;

    foreach (var innerKvp in innerDict)
    {
         Console.WriteLine(kvp.Key);
         Console.WriteLine(innerKvp.Key);
         Console.WriteLine(innerKvp.Value);
    }
}

【讨论】:

  • 非常感谢,现在我可以在您的帮助下继续前进
【解决方案2】:

您可以获得Names 列表,如下所示:

var namesList = management.SelectMany(x => x.Value.Select(y => y.Key));

现在只需调用上述列表中的First() 即可获取名字。

string temp = namesList.First();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    相关资源
    最近更新 更多