ramzan ali ramzan ali - 12 days ago 6
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;
student.GetFullName();

if (studentslist.Any(item => item.regNo == student.regNo) == false)
{
studentslist.Add(student);
regNoTextBox.Text = "";
firstNameTextBox.Text = "";
LastNameTextBox.Text = "";
MessageBox.Show("Information Saved.");
}
else
{
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

you can search for a student using unique registration value like

Student s=studentlists.Where(item=>item.regNo==textbox_regNumber).FirstorDefault();
if(s!=null)
{
   MessageBox.Show(s.studentName);
}
else
{
   MessageBox.Show("Not Found")
}
Comments