ramzan ali ramzan ali - 8 months ago 43
C# Question

How to find and show multiple items from a List using a single item?

//declaring new list
List<Student>studentslist=new List<Student>();
public Student student;
//Save Button
private void button1_Click(object sender, EventArgs e)
Student student = new Student();
student.regNo = regNoTextBox.Text;
student.firstName = firstNameTextBox.Text;
student.lastName = LastNameTextBox.Text;

if (studentslist.Any(item => item.regNo == student.regNo) == false)
regNoTextBox.Text = "";
firstNameTextBox.Text = "";
LastNameTextBox.Text = "";
MessageBox.Show("Information Saved.");
MessageBox.Show("The Reg.No is alresy exist !");

Here, I am adding unique Reg.No for the Students. Now I want to enter any saved Reg.No and then press Find Button then it will show corresponding student Name in MessageBox. How can I do so?

Answer Source

you can search for a student using unique registration value like

Student s=studentlists.Where(item=>item.regNo==textbox_regNumber).FirstorDefault();
   MessageBox.Show("Not Found")