Search a big generic list for an occurence of a string.

example code
you have:
objectA{
    public string aProperty{
       get{
          ...
       }
       set{
         ...
       }
   }
}

then

private bool ItemIncluded(ObjectA aItem){
    aItem.aProperty.Contains("SomeString");
}

list<ObjectA> result = AllObjects<ObjectA>.FindAll(ItemIncluded);

where AllObjects contains 200 000 entries (or more)

when I use the above described method it is unacceptably slow... is there a faster way to search strings and as you notice aProperty thus I can not assume that the value of aProperty is constant or does not change a lot.


Answer this question

Search a big generic list for an occurence of a string.

  • lathajith

    Have you tried using a datatable instead store all your objects in the datatable in one column (objectA) and store the property you want to search on (aProperty) in another column. Then you can filter the rows in the datatable based on your criteria as:

    mydatatable.Columns["aProperty"].Expression = "aProperty LIKE '*aa*'";

    or something like that. The idea is that since you're looking for text search, a datatable would probably be a better option (of course, you'll need to try it out before concluding :))

    hope that helps..
    Imran.

  • mhagman

    No - I didn't mean a database - I mean't a System.Data.DataTable. You can use the LIKE operator just like you would in a database the difference being that the DataTable is in-memory which will be faster.

    Imran.

  • BugInfested

    I dont use a database...

    I have found a serious bottleneck in my test setup... performance is now quit good.

    I think I am going to leave it like that develop some more and see how performance it holds. Its an application that doesnt really need to be scalable....

    The thing I want to be able to do is exactly like the search function in iTunes and that one is reaaly fast....

  • Steven Mooses

    If you have a known set of keywords, then when you add an object to list, you can index it on the keywords, e.g.,

    Each keyword is a key in a Hashtable.

    The value of the key is an ArrayList which contains the objects that contain the keyword in their aProperty.

    When you add an object to the list, scan the string for each keyword, adding the object to the appropriate keyword ArrayList.

    When you search on a keyword, check whether the object is on the appropriate keyword ArrayList (BinarySearch).

    You have thereby reduced your search space from the total number of objects, to the number of objects on the keyword ArrayList.


  • vefan

    there is no unique contraint on the property no...
    you can consider it more like a keyword to search on

  • luvzlabz

    I will look into that might be a good solution to link my Lists with databound controls

  • Sayhan

    By analogy to relational databases, you need to identify a suitable key on which you can search. Is there some part of aProperty which can act as a key

    Then you could use a System.Collections.Hashtable type to store your objects against the key.


  • lbeham

    If you have (for example) a class called dog with a property name I want to be able to look for the dogs that have a name that contais for example "aa". But I want to be able to change the name of the dog.

    so I can not sort on the name property because otherwise if I change it that means I have to rebuild whole my list.

    I do not think there is anyway of avoiding to iterate through whole the list. If i search on a boolean property it works quick only the strings are causing problems.



  • Search a big generic list for an occurence of a string.