Click here to Skip to main content
15,885,816 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have create a search list which shows the student details i need to find the student name with both upper case and lower case

Eg
student 1 name = arun
student 2 name = Aravid

if i enter the letter (a) in searchbar both arun and aravid should to be displayed

And i need to avoid the same name repeatation

If any one can correct my code please help me to figure it out

What I have tried:

private void search_box_TextChanged_1(object sender, TextChangedEventArgs e)
     {

         List<StudentItem> student= new List<StudentItem>();
         var prefix = search_box.Text;


         bool found = false;

         List<StudentItem> searchlist = DataAccess.getallstudent(empdetails);

         for (int i = 0; i < searchlist.Count; i++)
         {
             foreach (StudentItem item in searchlist)
             {
                 if (item.LastName.StartsWith(prefix))
                 {
                     found = true;


                     var eL_item = new StudentItem();
                     eL_item.FirstName = item.FirstName;
                     eL_item.LastName = item.LastName;
                     eL_item.TImageName = item.TImageName;

                     student.Add(eL_item);


                     studentlist.ItemsSource = empylast;

                 }


             }

         }

     }
Posted
Updated 6-Dec-22 2:58am
v2
Comments
PIEBALDconsult 6-Dec-22 10:57am    
As Richard said, or use Regular Expressions.

1 solution

Based on that code:
C#
List<StudentItem> searchlist = DataAccess.getallstudent(empdetails);
if (!string.IsNullOrEmpty(prefix))
{
    for (int i = searchlist.Count - 1; i >= 0; i--)
    {
        StudenItem item = searchlist[i];
        if (!item.LastName.StartsWith(prefix, StringComparison.CurrentCultureIgnoreCase))
        {
            searchlist.RemoveAt(i);
        }
    }
}

studentlist.ItemsSource = searchlist;

However, as I said last time you posted this question[^], it would be more efficient to pass the filter to the getallstudent method, and perform the filtering in the database. The only reason we can't tell you how to do that is that you haven't shown us the query you're using, nor described the structure of your tables.
 
Share this answer
 
Comments
Ailiseu Brigitta 7-Dec-22 0:00am    
sorry about that richard i got the answer so i didnt see the question i have posted
Ailiseu Brigitta 7-Dec-22 4:38am    
my doubt is how can i change (item.LastName )to (Item.firstname) by clicking the button i need to lastname as my default value
Richard Deeming 7-Dec-22 4:42am    
That depends. In the simple approach, have a bool searchLastName flag, and use that to determine which field to search:
StudentItem item = searchlist[i];
string fieldToSearch = searchLastName ? item.LastName : item.FirstName;
if (!fieldToSearch.StartsWith(prefix, StringComparison.CurrentCultureIgnoreCase))
{
    searchlist.RemoveAt(i);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900