【问题标题】:Trying to compare class fields C#尝试比较类字段 C#
【发布时间】:2021-12-28 02:36:35
【问题描述】:

下面我有两个类的构造函数。学生和课程

我正在尝试将 Student 类的 _completedCourses value 值与 Course 类的 coursecode 进行比较。

_completedCourses 是课程及其成绩的字典。我正在尝试查找未完成的课程并打印该列表。

我说的对吗?

public Course(string coursName, string courseCode, char passingGrade, double noOfCredits, Semester semesterOfferd, int major)
        {
            this.CourseName = coursName;
            this.CourseCode = courseCode;
            this.PassingGrade = passingGrade;
            this.NoOfCredits = noOfCredits;
            this.SemesterOfferd = semesterOfferd;
            this.prerequisiteCourses = new List<Course>();
            this._enrolledStudents = new List<Student>();
            this._major = major;
        }

 public Student(int studentID, string studentName, Status studentStatus, StudentMajor studentMajor)
        {
            this._studentID = studentID;
            this._studentName = studentName;
            this._studentStatus = studentStatus;
            this._studentMajor = studentMajor;
            this._course = new Course();
            this._completedCourses =  new Dictionary<string, char>();
            countStudent++;
        }

  public void RemainingCourses() 
        {
           var cKey = _completedCourses.ContainsKey(_course.CourseCode);
            Console.WriteLine("Remaining Courses");
            if (cKey.Equals(_course.CourseCode))
            {

                foreach (KeyValuePair<string, char> count in _completedCourses)
                {
                    {
                        {
                            Console.WriteLine(count.Key + " " + count.Value);
                            //          Console.WriteLine("Course "  + count.Value);
                            count.ToString();
                        }
                    }
                }
            }
        }

更新!!!

我的驱动程序类中的以下代码行实现了我想要的

Console.WriteLine("Enter Student ID ");
                    input = Convert.ToInt32(Console.ReadLine());
                    Console.Clear();
                    if (input.Equals(id.StudentID)) {
                        id.DisplayCompletedCourse();
                        foreach (var sub in isdCourses) {
                            var cKey = id.CompletedCourses.ContainsKey(sub.CourseCode);
                            if (!cKey)
                            { 
                                Console.WriteLine(sub.CourseCode);
                            }
                        }
                    }

【问题讨论】:

  • 嗨,我不确定,但看起来你可以做一个简单的 .Where 字典查询? (文档docs.microsoft.com/en-us/dotnet/api/…
  • 感谢您检查 Linq 以压缩代码并使其更具可读性

标签: c# dictionary equals


【解决方案1】:

ContainsKey 方法返回一个布尔值(真/假),所以在你的 if 条件下:

if (cKey.Equals(_course.CourseCode))

cKey 是一个布尔值,但_course.CourseCode 是一个字符串,所以这永远不会是真的,因此永远不会进入 if 块。

尝试像这样重写它:

public void RemainingCourses() 
{
    var cKey = _completedCourses.ContainsKey(_course.CourseCode);
    Console.WriteLine("Remaining Courses");
    if (cKey) // if cKey is "true", then the dictionary contains the CourseCode
    {
        foreach (KeyValuePair<string, char> count in _completedCourses)
        {
            Console.WriteLine(count.Key + " " + count.Value);
        }
    }
}

附带说明,请注意格式,您添加了不必要的花括号。此外,这可以使用 linq 进行简化,但我尽可能将其与原始代码保持一致。

【讨论】:

  • 欣赏小费。通过了
猜你喜欢
  • 2012-12-09
  • 2022-11-18
  • 1970-01-01
  • 2014-10-21
  • 2020-10-19
  • 2012-03-02
  • 2021-08-03
  • 1970-01-01
  • 2016-07-18
相关资源
最近更新 更多