Searching through a Listview with sqlite

Mohammad Ishtiaq 21 Reputation points
2021-11-26T20:30:16.177+00:00

I have an application that takes in user information like (Age, Gender, and Symptoms). I am successful at saving this information to an SQLite database after pressing a button that takes me to a new page which is a listview. This listview is Bound to a CS class I populated with some information. I am having trouble understanding how I can use the inputs from the SQLite database to filter through the list being displayed. I have provided the GitHub project here as well if you want to see it in action as of right now. Any help would be appreciated.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,814 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,901 Reputation points Microsoft Vendor
    2021-11-27T01:53:06.533+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Do you want to filter out the cases that meet all the conditions(Age, Gender, and Symptoms), and then return DNames to the Listview?

    For example, I want to filter the Age is 50, Gender is Male, DSymptoms is pain chest. We can use following code to filter it, Do not need to use ( ) to wrap all of the filter situation. we will get the two records High blood pressure, Diabetes in the listview.

       var  resList= _container.DiseaseDetails.Where(i => i.DSymptoms.ToLower().Contains(symptoms.ToLower()) && i.DAgeLow <= age && i.DAgeHigh >= age && i.MGender.Contains(gender) ).ToList();  
    

    And I notice you alway to get the first record in the tabel, If you want to get the lastest record that insert to the sqlite DB, you need to change the OnAppearing method's code from Dlistpage.cs

       protected override void OnAppearing()  
               {  
                   base.OnAppearing();  
         
                   using(SQLiteConnection conn = new SQLiteConnection(App.FilePath))  
                   {  
                       conn.CreateTable<PersonInfo>();  
                       var personinfo = conn.Table<PersonInfo>().ToList();  
         
                       int age = personinfo[personinfo.Count-1].PAge;  
                       string gender = personinfo[personinfo.Count - 1].PGender;  
                       string symptoms = personinfo[personinfo.Count - 1].PSymptoms;  
                       
                       if (symptoms == null)  
                       {  
                           symptoms = " ";  
                       }  
         
                       var _container = BindingContext as DViewModel;  
                       DisplayList.BeginRefresh();  
         
                       var  resList= _container.DiseaseDetails.Where(i => i.DSymptoms.ToLower().Contains(symptoms.ToLower()) && i.DAgeLow <= age && i.DAgeHigh >= age && i.MGender.Contains(gender) ).ToList();  
         
                       DisplayList.ItemsSource = resList;  //_container.DiseaseDetails.Where(i => (i.DSymptoms.ToLower().Contains(symptoms.ToLower())) && (i.DAgeLow <= age && i.DAgeHigh >= age) /*&& (i.MGender.Contains(gender))*/ );  
         
                       DisplayList.EndRefresh();  
                   }  
               }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful