Binding big database with observablecollection

quanlich

New member
Sep 11, 2012
41
0
0
Hi,

I'm currently writing a dictionary app using databound app template. My database is a sqlite database with more than 300.000 rows. Here is my code to store loaded database.

Code:
 public class Word
    {
        [PrimaryKey]
        public int Id { get; set; }
        
        public string WordString { get; set; }

        public string Content { get; set; }
    }

 public class WordList
    {
        public ObservableCollection<Word> Items { get; set; }

        public WordList()
        {
            Items = new ObservableCollection<Word>();
        }
    }

public class ViewModel {

//....
       public async void LoadData()
        {
            var query =  DB.Table<Word>();
            
            var result = await query.ToListAsync();
            

            foreach (Word w in result)
            {
                WList.Items.Add( new Word()
                {
                    Id = w.Id,
                    WordString = w.WordString,
                    Content = w.Content
                });
            }

            IsDataLoaded = true;
        }

}

And I use a TextBlock to display wordString and a phoneTextBox to search words.

Code:
 <toolkit:PhoneTextBox Hint="Search"
                                  TextChanged="SearchBox_TextChanged"
                                  Name="SearchBox"
                                  VerticalAlignment="Top" />

.....
<TextBlock Text="{Binding WordString}"
                                       FontSize="30"
                                       TextWrapping="Wrap"
                                       VerticalAlignment="Center"
                                       Margin="20,0,0,0"
                                       Foreground="Black"
                                       FontWeight="Bold"
                                       Grid.Row="0" />

And here is the code for searching:

Code:
....
 private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)        {
IList<Word> results = App.ViewModel.WList.Items
            .Where(tb =>tb.WordString.StartWith(SearchBox.Text))
            .ToList();

    
     ListBox1.ItemsSource = results;}

The problem is when I do a search, it takes to long to response.
Could you please give me a hint to accelerate my search? Could I create an indexer on ObservableCollection?

Thanks in advance.
 
Instead of using StartsWiith(),you could try using the overloaded IndexOf(string, startindex, count) for String. This is just an idea.

example: exstring.IndexOf(SearchBox.Text, 0, SearchBox.Text.Length);
 
Thank Catholic but it doesn't help much. It even made the app crash.

I tried to change observableCollection to other collections like Dictionary, List, HashShet... but I don't know why they do not display data on ListBox? Only ObservableCollection which functions!!
 

Trending Posts

Members online

No members online now.

Forum statistics

Threads
340,483
Messages
2,263,529
Members
428,793
Latest member
Glprobloods