簡體   English   中英

如何在列表中查找內容

[英]How to find something in a list

我有下一個程序。 我想如果您能向我解釋如何找到一個教室,例如我想找到一個教室,我該怎么做? 例如,我想添加已經添加到教室中的一門課程,並且在C#和列表(我來自C)方面還很新,所以我非常感謝您的幫助

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Maman15cs

{


public class ClassRoom
{

    public string ClassNumber;
    public int NumberofPlaces;
    public int[,] DayandHour = new int[6,8];

    public static ClassRoom AddClassRoom()
    {
        var classRoom = new ClassRoom();
        Console.WriteLine("Enter the Class number, the Number of places\n");
        classRoom.ClassNumber = Console.ReadLine().ToString();
        classRoom.NumberofPlaces = int.Parse(Console.ReadLine());
        Console.WriteLine("Good, now enter the Day(1, 2, 3, 4, 5, 6) and after that you put the courses' number that are that day (In Order)");
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 8; j++)
            {

               classRoom.DayandHour[i,j] = int.Parse(Console.ReadLine());

            }

        }
        return classRoom;
    }

    public static ClassRoom AddCourseInClassroom(ClassRoom classroom)
    {
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 8; j++)
            {

            }
        }
    }
}


public class Course
    {
        public string CourseName;
        public int CourseNumber;
        public int StudentsNumber;
        public string TeacherName;
        public string ClassNumber;



  public static Course AddCourse()
    {
        Course newCourse = new Course(); 
        Console.WriteLine("Enter the Course's name, course's number, students number, teacher's name, and class' number\n");
        newCourse.CourseName = Console.ReadLine().ToString();
        newCourse.CourseNumber = int.Parse(Console.ReadLine());
        newCourse.StudentsNumber = int.Parse(Console.ReadLine());
        newCourse.TeacherName = Console.ReadLine().ToString();
        newCourse.ClassNumber = Console.ReadLine().ToString();
        return newCourse;
    }
}

public class Program
{

     void Main()
    {
        var course = new List<Course>();
        var classroom = new List<ClassRoom>();



        int actionChoice;


         while(true){
         Console.WriteLine("What do you want to do? (Enter number): \n  1) Add a new Course \n 2)Add a new class room \n 3)Add an existing course to an existing classroom \n 4)Read the information of a specific classroom \n 5)Read the information of all the classrooms \n 6)Read the information of a specific course \n 7)Delete a specific course \n 8)Update courses in the Time Table \n 9)Exit the program  \n");
         actionChoice = int.Parse(Console.ReadLine());

         switch (actionChoice)
         {

             case 1: //Add a new Course

                 var new_course =  Course.AddCourse();                     
                 course.Add(new_course);

               break;

             case 2:

                var new_classRoom = ClassRoom.AddClassRoom();
                classroom.Add(new_classRoom);

               break;

             case 3:

               Console.WriteLine("Enter the course's number and the classroom's number");
               var courseNumber = int.Parse(Console.ReadLine());
               var classroomNumber = int.Parse(Console.ReadLine());

               course.Find(courseNumber);




               break;

             case 9:

               return;



         }


            }
        }
    }
}

如果可以使用LINQ,這很簡單:

var foundCourse = course.FirstOrDefault(c => c.CourseNumber == courseNumber)

if (foundCourse != null)
{
  // You're now interacting with the course we found
} 

如果您不能使用LINQ,請告訴我,我將向您介紹一種效率低下/干凈的方法。

編輯以顯示不同的方式:對於更簡單的方式(沒有LINQ),您可以只在for循環或foreach循環中進行操作。 例如:

private Course FindCourse(int courseNumber, List<Course> courses)
{
    foreach(Course course in courses)
    {
        if(course.CourseNumber == courseNumber)
            return course;
    }

    // Not found, return null
    return null;
}    

你可以這樣做

List<ClassRoom> result = yourlist.Where(x=>x.CourseName=="yourdesidredcourse").ToList();

如果您想了解C#中列表的api, 我將推薦此鏈接 但從本質上講,可以使用Where方法和謂詞(返回布爾值指示元素是否滿足特定條件的方法)輕松完成搜索。 考慮:

var matches = courses.Where( e => e.CourseNumber == providedCourseNumber );

此代碼段顯示:匹配項將是IEnumerable of Courses,其CourseNumber等於提供的CourseNumber。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM