Eugh...Array Search

2 posts, 0 answers
  1. Steve
    Steve avatar
    1871 posts
    Member since:
    Dec 2008

    Posted 28 Apr 2010 Link to this post

    I've got a textbox where I want a user to be able to type in a bunch of text and then I'll search my object collection for anything matching any of those values

    So if they search for "steve" it returns anything with "steve"

    however if they search for "steve april", it'll find anything with EITHER of those words

    How much of a pain in the ass is this to would one even do it...loop through each string and re-run the query over and over?

    (from r in scope.Extent<ViewAllRequest>() 
                                    where r.ProgramName.Contains(programName) && 
                                          (r.RotationName.Contains(searchText) || 
                                           r.ResidentName.Contains(searchText) || 
                                           r.SittingWith.Contains(searchText) || 
                                           r.TypeName.Contains(searchText) || 
                                           r.HospitalName.Contains(searchText) || 
                                           r.AcademicYear.ToString().Contains(searchText) || 
                                           r.Comments.Contains(searchText) || 
                                    orderby r.SubmittedDate descending 
                                    select r).ToList(); 

  2. PetarP
    PetarP avatar
    754 posts

    Posted 30 Apr 2010 Link to this post

    Hi Steve,

    In order to avoid the execution of the same query multiple times you will have to modify your query a little bit. First you will need to take your input string and spit it to separate words. This can be done with the following line of code:
    List<string> wordsToSearchFor = inputString.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).ToList();
    Now that you have a list of words for which you can perform the search the only thing left for you is to modify the query in the following way:
    (from r in scope.Extent<ViewAllRequest>()
                 where r.ProgramName.Contains(programName) &&
    (wordsToSearchFor.Contains(r.RotationName) ||
                       wordsToSearchFor.Contains(r.ResidentName) ||
    wordsToSearchFor.Contains(r.SittingWith) ||
    wordsToSearchFor.Contains(r.TypeName) ||
    wordsToSearchFor.Contains(r.HospitalName) ||
    wordsToSearchFor.Contains(r.AcademicYear.ToString()) ||
    wordsToSearchFor.Contains(r.Comments) ||
                 orderby r.SubmittedDate descending
                 select r).ToList();
    I hope that helps.

    Sincerely yours,
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top