-1

我想将项目添加到包含字典的字典中,请参阅下面的代码。

Dictionary<string, Dictionary<string, int>> dict = new Dictionary<string, Dictionary<string, int>>();
foreach (var item in TrainingCourseList)
{
    // I have to add item.ID,item.Name,item.Score something like below
    dict.Add(item.ID,item.Name,item.Score);
}
4

3 回答 3

0

如果要向外部字典添加条目,则必须执行类似的操作

Dictionary<string, Dictionary<string, int>> dict = new Dictionary<string, Dictionary<string, int>>();
    foreach (var item in TrainingCourseList)
    {
        // i have to add item.ID,item.Name,item.Score something like below
        dict.Add(item.ID,new Dictionary<string,int>{{item.Name,item.Score}};
    }
于 2013-01-31T12:07:23.430 回答
0

尝试

Dictionary<string, Dictionary<string, int>> dict = new Dictionary<string, Dictionary<string, int>>();
foreach (var item in TrainingCourseList)
{
    // i have to add item.ID,item.Name,item.Score something like below
    dict.Add(item.ID, new Dictionary<string, int> { item.Name, item.Score });
}
于 2013-01-31T12:10:01.450 回答
0

我想Tuple<string, string, int>这是最适合您的情况,而不是字典:

var list = TrainingCourseList
    .Select(item => Tuple.Create(item.ID, item.Name, item.Score));
于 2013-01-31T12:10:33.043 回答