【问题标题】:How can i search in c# list with one of its information?如何在 c# 列表中搜索其信息之一?
【发布时间】:2021-01-15 11:24:49
【问题描述】:

所以我的程序中有一个对象列表,其中的对象具有(姓名、姓氏、id、房子等) 我想创建一个名为 search 的方法,它可以让我仅通过其名称查找特定对象。我想在搜索后打印出信息。 (我有一个家长班被称为向导和两个孩子班。一个给老师,一个给学生(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));

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

我的课程是: 我的父类

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);
        }
    }
}

我的孩子班:

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)
        {

        }
     
    }
}
 

【问题讨论】:

  • WizardStudents.Where(s => s.name == somename); 可能有用,不知道你还没有发布这些课程。
  • 是的,我想找一个有名字的学生。
  • 我想在我的列表中搜索,只有用户输入他们的名字

标签: c# list inheritance search console


【解决方案1】:

使用语言集成查询来搜索列表。以下是文档链接: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();
        }        
    }
}

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

【讨论】:

  • 我可以在我的孩子班级中使用这个设置吗?
  • 让我将我的课程添加到我的问题中
  • 是的,在 C# 中,get、set 的那一行被称为 Properties。是的,您可以在您的孩子课堂上使用它们。您不必在此处指定类。只需将我的代码复制到控制台项目中并运行它以获取想法,然后相应地修改您的代码。
【解决方案2】:

首先覆盖WizardTobe 类中的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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-27
    • 2016-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多