[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 565
  • Last Modified:

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?
0
Maged-Obied
Asked:
Maged-Obied
  • 3
  • 3
1 Solution
 
käµfm³d 👽Commented:
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

0
 
Maged-ObiedAuthor Commented:
'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?)      
0
 
käµfm³d 👽Commented:
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Maged-ObiedAuthor Commented:
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()
0
 
käµfm³d 👽Commented:
So it was. 4 AM is way past my bedtime, so let's see if I can get this right now that I've had some sleep  ; )

We needed Value, not Values:

if (kvpList[i].Value.StartsWith(Searchtxt)) keys.Add(kvpList[i].Value);

Open in new window

0
 
Maged-ObiedAuthor Commented:
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now