在 if 和 foreach 语句中使用列表

Using lists within an if and foreach statement

我正在创建一个控制台应用程序,它使用用户输入来确定两个列表中的数据。每个列表都有相同的数据对象。它们是姓名、rollNo、course 和 stream。

当用户输入所有数据后,系统会询问他们希望看到哪些流信息。然后我想要显示与流输入相关的数据。例如,如果他们想要查看流 1,则该流上的培训师和学生的信息应该出现在控制台中。

下面是我的代码。

            Student studentOne = new Student();
            Console.WriteLine("Enter the Name of Student:");
            studentOne.name = Console.ReadLine();
            Console.WriteLine("Enter the roll number of Student:");
            studentOne.rollNo = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the course of Student:");
            studentOne.course = Console.ReadLine();
            Console.WriteLine("Enter the Stream of Student:");
            studentOne.stream = int.Parse(Console.ReadLine());

            Student studentTwo = new Student();
            Console.WriteLine("Enter the Name of Student:");
            studentTwo.name = Console.ReadLine();
            Console.WriteLine("Enter the roll number of Student:");
            studentTwo.rollNo = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the course of Student:");
            studentTwo.course = Console.ReadLine();
            Console.WriteLine("Enter the Stream of Student:");
            studentTwo.stream = int.Parse(Console.ReadLine());

            Student studentThree = new Student();
            Console.WriteLine("Enter the Name of Student:");
            studentThree.name = Console.ReadLine();
            Console.WriteLine("Enter the roll number of Student:");
            studentThree.rollNo = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the course of Student:");
            studentThree.course = Console.ReadLine();
            Console.WriteLine("Enter the Stream of Student:");
            studentThree.stream = int.Parse(Console.ReadLine());

            var studentData = new List<Student>();
            studentData.Add(studentOne);
            studentData.Add(studentTwo);
            studentData.Add(studentThree);

            Trainer trainerOne = new Trainer();
            Console.WriteLine("Enter the Name of the trainer:");
            trainerOne.name = Console.ReadLine();
            Console.WriteLine("Enter the roll number of the trainer:");
            trainerOne.trainNo = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the course of the trainer:");
            trainerOne.course = Console.ReadLine();
            Console.WriteLine("Enter the Stream of the trainer:");
            trainerOne.stream = int.Parse(Console.ReadLine());

            Trainer trainerTwo = new Trainer();
            Console.WriteLine("Enter the Name of the trainer:");
            trainerTwo.name = Console.ReadLine();
            Console.WriteLine("Enter the roll number of the trainer:");
            trainerTwo.trainNo = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the course of the trainer:");
            trainerTwo.course = Console.ReadLine();
            Console.WriteLine("Enter the Stream of the trainer:");
            trainerTwo.stream = int.Parse(Console.ReadLine());

            Trainer trainerThree = new Trainer();
            Console.WriteLine("Enter the Name of the trainer:");
            trainerThree.name = Console.ReadLine();
            Console.WriteLine("Enter the roll number of the trainer:");
            trainerThree.trainNo = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter the course of the trainer:");
            trainerThree.course = Console.ReadLine();
            Console.WriteLine("Enter the Stream of the trainer:");
            trainerThree.stream = int.Parse(Console.ReadLine());

            List<Trainer> trainerData = new List<Trainer>();
            trainerData.Add(trainerOne);
            trainerData.Add(trainerTwo);
            trainerData.Add(trainerThree);

            Console.WriteLine("Which stream details do you want to view?");
            var streamInfo = int.Parse(Console.ReadLine());

            if (streamInfo == 1)
            {
                foreach (var stream in trainerData) 
                {

                }
            }

    class Student
    {
        public string name { get; set; }
        public int rollNo { get; set; }
        public string course { get; set; }
        public int stream { get; set; }
        
    }

    class Trainer
    {
        public string name { get; set; }
        public int trainNo { get; set; }
        public string course { get; set; }
        public int stream { get; set; }

    }

感谢任何帮助。

编辑:我没有说清楚我的要求。我不确定如何根据流输入的内容显示正确的数据。我试过使用 if 语句和 foreach 语句,但我不知道这是否是最好的方法。

分解解法,从读整数开始:

private static int ReadInteger(string title, Func<int, bool> isValid = null) {
  if (isValid is null)
    isValid = x => true;  

  while (true) {
    if (!string.IsNullOrWhiteSpace(title))
      Console.WriteLine(title);

    if (int.TryParse(Console.ReadLine(), out int result) && isValid(result))
      return result;

    Console.WriteLine("Invalid value, please, try again.");
  }
}

private static string ReadString(string title, Func<string, bool> isValid = null) {
  if (isValid is null)
    isValid = x => true;  

  while (true) {
    if (!string.IsNullOrWhiteSpace(title))
      Console.WriteLine(title);

    string result = Console.ReadLine(); 

    if (isValid(result))
      return result;

    Console.WriteLine("Invalid value, please, try again.");
  }
}

然后我们可以实现ReadStudentReadTrainer:

public static Student ReadStudent() {
  return new Student() {
    name   = ReadString("Enter the Name of Student:", x => !string.IsNullOrWhiteSpace(x)),
    rollNo = ReadInteger("Enter the roll number of Student:", x => x >= 0),
    course = ReadString("Enter the course of Student:", x => !string.IsNullOrWhiteSpace(x)),
    stream = ReadInteger("Enter the Stream of Student:", x => x >= 0),
  };
}

public static Trainer ReadTrainer() {
  return new Trainer() {
    name   = ReadString("Enter the Name of trainer:", x => !string.IsNullOrWhiteSpace(x)),
    rollNo = ReadInteger("Enter the roll number of trainer:", x => x >= 0),
    course = ReadString("Enter the course of trainer:", x => !string.IsNullOrWhiteSpace(x)),
    stream = ReadInteger("Enter the Stream of trainer:", x => x >= 0),
  };
}

最后,我们可以创建任意数量的学生和培训师(注意 for loop - 如果我们想输入 100 学生怎么办? ):

int students = 3;
int trainers = 3;

var studentData = new List<Student>(students);

for (int i = 0; i < students; ++i)
  studentData.Add(ReadStudent());

var trainerData = new List<Trainer>(trainers);

for (int i = 0; i < trainers; ++i)
  trainerData.Add(ReadTrainer());

最后,要按流查询列表,您可以使用 Linq 或良好的旧循环或 Linq:

int stream = ReadInteger("Which stream details do you want to view?");

foreach (Student s in studentData)
  if (s.stream == stream)
    Console.WriteLine($"Student {s.name} Roll No {s.rollNo}");

foreach (Trainer t in trainerData)
  if (t.stream == stream)
    Console.WriteLine($"Trainer {t.name} Roll No {t.rollNo}");