Search code examples
c#sqlasp.net-mvcentity

ASP.NET MVC Entity Framework query search


How can I search data from single input like Hulk 2003 from SQL database when there are two columns in the database (Movie_title and Release_Date).

Just to be clear column Movie_title contains just the name of the movie and the release date of that movie is the column Release_Date.

I tried with

 public ActionResult Search(string search)
 {
     if (search == null)
     {
         return View("Index");
     }

     var Movie = db.Movies.Where(m => m.Movie_title .Contains(search) || 
                                      m.Release_date.Contains(search))
                          .ToList();

     return View(Movie);
}

Solution

  • I manage to find solution, if you have better please post it ;)

      public ActionResult Search(string search)
        {
            int year = 0;
            string title = string.Empty;
            string yearparse = string.Empty;
            if (string.IsNullOrWhiteSpace(search))
            {
                return View("Index");
            }
    
            string movieName = search;
            string movieYear = search;
    
            for (int i = 0; i < movieName.Length; i++)
            {
                if (char.IsLetter(movieName[i]))
                {
                    title += movieName[i];
                }
            }
            for (int i = 0; i < movieYear.Length; i++)
            {
                if (char.IsDigit(movieYear[i]))
                {
                    yearparse += movieYear[i];
                    year = int.Parse(yearparse);
                }
            }
    
            var Movies = db.Movies.Where(m => m.Movie_Title.Contains(title) && m.Release_date.Contains(year.ToString())).ToList();
    
            return View(Movies);
    
        }