0

my code only works if I search correctly for the Name from the table. I must search for the FULL name and spell it corretly with uppercase etc. E.g. I Cannot search for 'The Martian' by searching 'the martian' or 'martian' etc.

using(MovieEntities db = new MovieEntities()){

   var searchMovie = new List<Movie>(db.Moviess.ToList());
            var searchFilter = new List<Movie>();

            foreach (var search in searchMovie)
            {
                if (search.Name.Contains(txtSearch.Text))
                {

                    searchFilter.Add(search); 
                   //so far, only adds if I search it's full/correctly name

                }
            }/* print out */}

How can I search if it contains ANY parts of the txtSearch.Text and also ignoring under-/uppercase etc ?

PS: Im trying to learn about LINQ, I would appreciate if you also could give an alternative Linq solution.

Thanks

Ezony
  • 141
  • 1
  • 3
  • 9

2 Answers2

1

This will keep the searches on the database side, which will speed them up, and most people have their databases configured to be case insensitive, so you get that as a freebie.

using(MovieEntities db = new MovieEntities()){
  var searchFilter=db.Moviess.AsQueryable();
  foreach(var word in txtSearch.Text.Split(' '))
  {
    searchFilter=searchFilter.Where(f=>f.Name.Contains(word));
  }
  /* Print */  

Although you asked for "How can I search if it contains ANY parts of the txtSearch.Text", which isn't what this does. It makes sure it contains all words of txtSearch.Text, in any order which is more typical usage. It can be rewritten to any part as well, but then "the anything" would return an awful lot of results.

Robert McKee
  • 21,305
  • 1
  • 43
  • 57
  • Does EF really support `.Contains` expressions? Last time I used it wouldn't let me. Admittedly, that was a very long time ago - it may be updated now. – Gediminas Masaitis Mar 08 '16 at 22:19
  • Pretty sure it does now. It will convert to either a LIKE %..% or a charindex(...) depending on a number of factors. What it doesn't (or didn't ) support was Contains taking two parameters to support forcing case insensitive/sensitive versions. – Robert McKee Mar 08 '16 at 22:22
  • @RobertMcKee I've tested it with EF7, and indeed it works as expected. This makes your answer the correct way to go when using an up-to-date version of EF! I'm sorry my answer got approved while I didn't know this. – Gediminas Masaitis Mar 09 '16 at 00:13
  • It worked in EF 6.x as well. I can't comment on versions prior to that. – Robert McKee Mar 09 '16 at 17:50
0

Your query should already search for any strings which include your txtSearch.Text, since you used Contains instead of Equals. The only thing that's missing is to make it case-insensitive. You can do so by essentially making both your strings lowercase (or uppercase) using either the String.ToLower or String.ToUpper methods on both your strings.

So in your case, it should be:

if (search.Name.ToLower().Contains(txtSearch.Text.ToLower()))
{
    // ...
}
Gediminas Masaitis
  • 3,172
  • 14
  • 35