C# 当其中一个属性是列表时,如何迭代字典

C# 当其中一个属性是列表时,如何迭代字典,c#,C#,我制作了一本字典: Dictionary<int, Student> students = new Dictionary<int, Student>(); foreach (KeyValuePair<int, Student> student in students) { resultLabel.Text = String.Format("<br />Student ID: {0} Student Nam

我制作了一本字典:

Dictionary<int, Student> students = new Dictionary<int, Student>();
foreach (KeyValuePair<int, Student> student in students)
        {
            resultLabel.Text = String.Format("<br />Student ID: {0}  Student Name: {1} ", student.Key, ???);
Dictionary学生=newdictionary();
在下列班级中:

public class Student
    {
        public int StudentId { get; set; }
        public string Name { get; set; }
        public List<Course> Courses { get; set; }
    }
公共班级学生
{
公共int StudentId{get;set;}
公共字符串名称{get;set;}
公共列表课程{get;set;}
}
学生ID是关键。现在,我想反复阅读字典,打印每个学生ID和学生姓名,以及每个学生注册的课程列表。我知道如何反复阅读字典:

Dictionary<int, Student> students = new Dictionary<int, Student>();
foreach (KeyValuePair<int, Student> student in students)
        {
            resultLabel.Text = String.Format("<br />Student ID: {0}  Student Name: {1} ", student.Key, ???);
foreach(学生中的KeyValuePair学生)
{
resultLabel.Text=String.Format(“
学生ID:{0}学生姓名:{1}”,学生.Key,?);

但是我不知道如何1)打印学生,2)迭代并打印每个学生的课程列表。如果有任何帮助,我将不胜感激。

正如您问题的评论中所建议的,更好地理解键值对和迭代器可能是有益的。尽管如此,当您有一个嵌套结构时,如图所示如果要在一行中打印出来,则
String.Join
函数很方便

foreach (KeyValuePair<int, Student> student in students)
{
     var courses = String.Join(", ", student.Value.Courses);
     var output = String.Format("ID: {0}, Name: {1}, Courses: {2}", student.Key, student.Value.Name, courses);

    // Alternatively you could iterate over the courses here
    // foreach (Course course in student.Value.Courses) ...

     resultLabel.Text = String.Concat("<br />", output);
}
foreach(学生中的KeyValuePair学生)
{
var courses=String.Join(“,”,student.Value.courses);
var output=String.Format(“ID:{0},Name:{1},Courses:{2}”,student.Key,student.Value.Name,Courses);
//或者,你可以在这里反复学习这些课程
//foreach(学生价值观课程)。。。
resultLabel.Text=String.Concat(“
”,输出); }
试试这样的方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            List<Student> students = new List<Student>() {
                   new Student(new List<int>() {100,103,106}) { StudentId = 1001, Name = "John"}, 
                   new Student(new List<int>() {101,104,105}) { StudentId = 1002, Name = "Mary"},
                   new Student(new List<int>() {102,105,104}) { StudentId = 1003, Name = "Tom"},
                   new Student(new List<int>() {103,106,102}) { StudentId = 1004, Name = "Dick"},
                   new Student(new List<int>() {104,101,102}) { StudentId = 1005, Name = "Harry"}
                };

            Dictionary<int, Student> dictStudents = students.AsEnumerable()
                .GroupBy(x => x.StudentId, y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            foreach (KeyValuePair<int, Student> student in dictStudents)
            {
                string message = String.Format("<br />Student ID: {0}  Student Name: {1} Courses: {2}", 
                    student.Key.ToString(),
                    student.Value.Name,
                    string.Join(",", student.Value.Courses.Select(x => "CourseID=" + x.CourseID.ToString() + ":" + "Course Name= " + x.Name).ToArray()));
                Console.WriteLine(message);
            }
            Console.ReadLine();

        }
    }
    public class Student
    {
        public int StudentId { get; set; }
        public string Name { get; set; }
        public List<Course> Courses { get; set; }

        public Student() { Courses = new List<Course>();}
        public Student(List<int> couseNumbers)
        {
            Courses = new List<Course>();
            foreach(int id in couseNumbers)
            {
                Courses.Add(Course.courses.Where(x => x.CourseID == id).FirstOrDefault()); 
            }
        }
    }
    public class Course
    {
        public static List<Course> courses = new List<Course>() {
                new Course() {CourseID = 100, Name = "abc"},
                new Course() {CourseID = 101, Name = "abd"},
                new Course() {CourseID = 102, Name = "abe"},
                new Course() {CourseID = 103, Name = "abf"},
                new Course() {CourseID = 104, Name = "abg"},
                new Course() {CourseID = 105, Name = "abh"},
                new Course() {CourseID = 106, Name = "abi"}
        };

        public int CourseID { get; set; }
        public string Name { get; set; }
    }
}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
学生名单=新名单(){
新学生(new List(){100103106}{StudentId=1001,Name=“John”},
新学生(new List(){101104105}{StudentId=1002,Name=“Mary”},
新学生(new List(){102105104}){StudentId=1003,Name=“Tom”},
新学生(new List(){103106102}{StudentId=1004,Name=“Dick”},
新学生(new List(){104101102}{StudentId=1005,Name=“Harry”}
};
Dictionary dictStudents=students.AsEnumerable()
.GroupBy(x=>x.StudentId,y=>y)
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
foreach(dictStudents中的KeyValuePair学生)
{
string message=string.Format(“
学生ID:{0}学生姓名:{1}课程:{2}”, student.Key.ToString(), student.Value.Name, string.Join(“,”,student.Value.Courses.Select(x=>“CourseID=“+x.CourseID.ToString()+”:“+”课程名称=“+x.Name).ToArray()); 控制台写入线(消息); } Console.ReadLine(); } } 公立班学生 { 公共int StudentId{get;set;} 公共字符串名称{get;set;} 公共列表课程{get;set;} 公立学生(){Courses=new List();} 公立学生(名单成员) { 课程=新列表(); foreach(表中的int-id) { Courses.Add(Course.Courses.Where(x=>x.CourseID==id).FirstOrDefault()); } } } 公共课 { 公共静态列表课程=新列表(){ 新课程(){CourseID=100,Name=“abc”}, 新课程(){CourseID=101,Name=“abd”}, 新课程(){CourseID=102,Name=“abe”}, 新课程(){CourseID=103,Name=“abf”}, 新课程(){CourseID=104,Name=“abg”}, 新课程(){CourseID=105,Name=“abh”}, 新课程(){CourseID=106,Name=“abi”} }; public int CourseID{get;set;} 公共字符串名称{get;set;} } } ​
您正在寻找
student.Value
来代替
。只需转到
student.Value.Name
等。对于课程,它是相同的:使用
foreach(在student.Value.courses中的课程课程)
foreach(在student.Values中的学生学生)…如果您确实无法理解如何检索由
foreach
循环枚举的
KeyValuePair
实例的值组件,那么您对C的理解就比单堆栈溢出答案中可以合理解决的要深得多。我建议您在学习如何声明C#对象,阅读
字典
键值对
的文档,以便您了解它们是如何工作的,以及它们之间的关系。谢谢Asad。我不知道我是如何忽略了这一点的。