我如何在 c# 列表中搜索其信息之一?

How can i search in c# list with one of its information?

所以我的程序中有一个 object 的列表,其中 object 有(姓名、姓氏、身份证、房子等) 我想创建一个名为 search 的方法,让我仅通过其名称即可找到特定的 object。我想在搜索后打印出信息。 (我有一个 parent class caled wizard 和两个 child classes。一个给老师,一个给学生 (WizardToBe)。)

List<WizardTobe> WizardStudents = new List<WizardTobe>();

我使用 Readline 收到了下面所需的所有信息,因此所有这些都是用户输入的。 我所有的代码都在一个 while 循环中。

                        string studentname = Console.ReadLine();
                        Console.WriteLine("Your last name?");
                        string studentlast = Console.ReadLine();
                        Console.WriteLine("And you my child, which house are you from?");
                        string studentHouse = Console.ReadLine();
                        Console.WriteLine("And may i know your wizard ID?");
                        string studentWizID = Console.ReadLine();
                        Console.WriteLine("And what creature is your lovely Familiar?");
                        string studentFamiliar = Console.ReadLine();
                        Console.WriteLine("What is your student ID?");
                        string babywizID = Console.ReadLine();
                        Console.WriteLine("sorry this is taking long. Lastly! in School year, what year are you curently in?");
                        string yearschool = Console.ReadLine();
                        WizardStudents.Add(new WizardTobe(studentname, studentlast, studentWizID, studentHouse, studentFamiliar, babywizID, yearschool));

我试图将我想添加到我的列表中的 object 命名为“studentname”,这是我通过 Readline 收到的。但我遇到了另一个错误...... 我应该怎么办 ? :(

我的class是这些: 我的 parent class

using System;
using System.Collections.Generic;
using System.Text;

namespace HogwartsSignUp
{
    public class Wizard
    {
        string name;
        string lastname;
        string wizID;
        string house;

        //teacher
        private string yearclass;
        private string workID;

        //students
        private string familiar;
        private string studentID;
        private string studentyear;




        public Wizard(string name, string lastname, string wizID, string house, string familiar, string studentID, string studentyear)
        {
            this.name = name;
            this.lastname = lastname;
            this.wizID = wizID;
            this.house = house;
            this.studentID = studentID;
            this.familiar = familiar;
            this.studentyear = studentyear;
        }
        public Wizard(string name, string lastname, string wizID, string house, string yearclass, string workID)
        {
            this.name = name;
            this.lastname = lastname;
            this.wizID = wizID;
            this.house = house;
            this.workID = workID;
            this.yearclass = yearclass;
        }
        public void showStudentInfo()
        {
            Console.WriteLine("They are a Hogwarts student! Here's the information of them:");
            Console.WriteLine("Name: {0}     Last name: {1}       House: {2}        Year: {3}      Familiar: {4}", name, lastname,house,studentyear,familiar);
        }

        public void showTeacherInfo()
        {
            Console.WriteLine("They are a Hogwarts teacher! Here's the information of them:");
            Console.WriteLine("Name:{0}     Last Name:{1}        House:{2}      Teacher of the class: {3}", name, lastname, house, yearclass);
        }
    }
}

我的childclass:

using System;
using System.Collections.Generic;
using System.Text;

namespace HogwartsSignUp
{
    public class WizardTobe : Wizard
    {
        string magicwand;
        string sportinterest;
        string hobby;


        public WizardTobe(string name, string lastname, string wizID, string house, string familiar, string studentID, string studentyear) : base (name, lastname,  wizID, house, familiar, studentID, studentyear)
        {

        }
     
    }
}
 

首先,在您的 WizardTobe class、return 字符串中重写 ToString() 函数,其中包含您要打印的所有属性。

public class WizardTobe
{
   public string StudentName{ get; set; }
   public string StudentLastName{ get; set; }
   ...
   public override string ToString()
   {
       return $"Student First Name: {StudentName}: Last Name: {StudentLastName}";
   } 
}

在您要打印此信息的程序中,根据您的要求使用 .Where().First().Single()

我正在使用 .Where() 因为可能有多个同名学生,让我们打印所有满足此条件的学生

var results = WizardStudents.Where(s => s.name == somename);
foreach(var result in results)
    Console.WriteLine(result);

MSDN :How to override the ToString method

使用语言集成查询搜索列表。以下是文档的 link:https://docs.microsoft.com/en-us/dotnet/csharp/linq/

这是一个示例控制台应用程序,使用您的代码来说明这一点。

using System;
using System.Collections.Generic;
using System.Linq;

namespace Console_Example
{
    public class WizardTobe
    {
        public string StudentName { get; set; }
        public string StudentLast { get; set; }
        public string StudentWizID { get; set; }
        public string StudentHouse { get; set; }        
        public string StudentFamiliar { get; set; }
        public string BabyWizID { get; set; }
        public string YearsSchool { get; set; }

        public WizardTobe(string studentName, string studentLast, string studentWizID, string studentHouse, string studentFamiliar, string babyWizID, string yearsSchool)
        {
            StudentName = studentName;
            StudentLast = studentLast;
            StudentWizID = studentWizID;
            StudentHouse = studentHouse;            
            StudentFamiliar = studentFamiliar;
            BabyWizID = babyWizID;
            YearsSchool = yearsSchool;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            List<WizardTobe> WizardStudents = new List<WizardTobe>();

            string studentname = Console.ReadLine();
            Console.WriteLine("Your last name?");
            string studentlast = Console.ReadLine();
            Console.WriteLine("And you my child, which house are you from?");
            string studentHouse = Console.ReadLine();
            Console.WriteLine("And may i know your wizard ID?");
            string studentWizID = Console.ReadLine();
            Console.WriteLine("And what creature is your lovely Familiar?");
            string studentFamiliar = Console.ReadLine();
            Console.WriteLine("What is your student ID?");
            string babywizID = Console.ReadLine();
            Console.WriteLine("sorry this is taking long. Lastly! in School year, what year are you curently in?");
            string yearschool = Console.ReadLine();
            WizardStudents.Add(new WizardTobe(studentname, studentlast, studentWizID, studentHouse, studentFamiliar, babywizID, yearschool));
            WizardStudents.Add(new WizardTobe("Peter","","","","","",""));
            WizardStudents.Add(new WizardTobe("Steven", "", "", "", "", "", ""));
            WizardStudents.Add(new WizardTobe("Maria", "", "", "", "", "", ""));
            WizardStudents.Add(new WizardTobe("Ashley", "", "", "", "", "", ""));

            string studentToBeSearched = "Ashley";
            Console.WriteLine($"\n\nSearching for student named {studentToBeSearched}");
            WizardTobe missingStudent = WizardStudents.Where(x => x.StudentName == studentToBeSearched).FirstOrDefault();
            Console.WriteLine(missingStudent.StudentName);

            Console.WriteLine("Press Any Key");
            Console.ReadKey();
        }        
    }
}

注意:以上代码仅用于演示问题的解决方案,并非用于演示良好的编程习惯。