Link to home
Start Free TrialLog in
Avatar of Maged-Obied
Maged-Obied

asked on

Filtered List Box in .Net CF & Windows Mobile 5.0

i have a list box with over 300 items, and text box to filter these items on a user input, and i am using the following code:

          private void textBox1_TextChanged(object sender, EventArgs e)
        {
         
            listBox1.DataSource = null;
            string Searchtxt = textBox1.Text;
            List<string> keys = kvpList
               .Where(kvp => kvp.Value.ToString().StartsWith(Searchtxt))
               .Select(kvp => kvp.Value)
               .ToList();

            listBox1.DataSource = new BindingSource(keys, null);
          }
my problem is that this code is taking long time (3-4 sec and sometimes more) to get the filtered list.

i wanna know what is the problem with this code, is it the keyvalue pair i am using or its a performance issue from the device, or maybe it is the code itself.

can any one suggest how to do this filtration with a different way that doesn't have this performance issue?
Avatar of kaufmed
kaufmed
Flag of United States of America image

What if you used a simple for loop instead of Linq?

List<string> keys = new List<string>(kvpList.Values.Count);

for (int i = 0; i < kvpList.Values.Count; i++)
{
    if (kvpList.Values[i].StartsWith(Searchtxt)) keys.Add(kvpList.Values[i]);
}

Open in new window

Avatar of Maged-Obied
Maged-Obied

ASKER

'System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string,string>>' does not contain a definition for 'Values' and no extension method 'Values' accepting a first argument of type 'System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string,string>>' could be found (are you missing a using directive or an assembly reference?)      
Ah. Remove ".Values":

List<string> keys = new List<string>(kvpList.Count);

for (int i = 0; i < kvpList.Count; i++)
{
    if (kvpList[i].StartsWith(Searchtxt)) keys.Add(kvpList[i]);
}

Open in new window

also this one is not working, the kvplist is showing in the list box as key and value and i need to show the value only and this was done by LINQ.
 kvp.Value.ToString()
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ya this one is working, but still the same performance; thanks any way for your help.

maybe its a device problem, because the original code is working fine on another device but its CE.

while debugging the delay time coming from the loop.(new code with loop instead of LINQ)

my question now how can i do this search with any other way? if there is a way.