This is a migrated thread and some comments may be shown as answers.

Eugh...Array Search

1 Answer 37 Views
LINQ (LINQ specific questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
sitefinitysteve asked on 28 Apr 2010, 07:28 PM
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 do...how 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) || 
                                       r.RequestID.ToString().ToLower().Contains(searchText)) 
                                orderby r.SubmittedDate descending 
                                select r).ToList(); 

1 Answer, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 30 Apr 2010, 11:15 AM
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) ||
wordsToSearchFor.Contains(r.RequestID.ToString()))
             orderby r.SubmittedDate descending
             select r).ToList();
I hope that helps.

Sincerely yours,
Petar
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.
Tags
LINQ (LINQ specific questions)
Asked by
sitefinitysteve
Top achievements
Rank 2
Iron
Veteran
Answers by
PetarP
Telerik team
Share this question
or