ASP. NET MVC5 -- Contains, mvc5 -- contains
Preface:
* The Contains method is run on the database, not the c # code above. On the database, Contains maps to SQL LIKE, which is case insensitive.
This statement indicates that the Contains () method runs in the database, instead of the C # code. In the database, Contains is LIKE the LIKE keyword in SQL, Which is case sensitive.
I. We will add a fuzzy query for the Index page and find the Index method in the controller. The modification is as follows:
1 public ActionResult Index(string searchString) 2 { 3 4 var movies = from m in db.Movies select m; 5 6 if (!string.IsNullOrEmpty(searchString)) 7 { 8 movies = movies.Where(s => s.Title.Contains(searchString)); 9 }10 return View(movies);11 }
Executed
var movies = from m in db.Movies select m;
After this code is run, the SQL statement executed by movies is:
1 SELECT 2 [Extent1].[ID] AS [ID], 3 [Extent1].[Title] AS [Title], 4 [Extent1].[ReleaseDate] AS [ReleaseDate], 5 [Extent1].[Genre] AS [Genre], 6 [Extent1].[Price] AS [Price] 7 FROM [dbo].[Movies] AS [Extent1]
After executing this sentence, the movies SQL statement is:
1 SELECT 2 [Extent1].[ID] AS [ID], 3 [Extent1].[Title] AS [Title], 4 [Extent1].[ReleaseDate] AS [ReleaseDate], 5 [Extent1].[Genre] AS [Genre], 6 [Extent1].[Price] AS [Price] 7 FROM [dbo].[Movies] AS [Extent1] 8 WHERE [Extent1].[Title] LIKE @p__linq__0 ESCAPE N'~'
As you can see, the Contains method generates the statement after Like.