前言:
* 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.
这句话的意思是Contains()方法,运行在数据库中,而不是C#代码上,在数据库中,Contains就像SQL中的LIKE关键字,它是大小写敏感的。
一:我们来为Index页面添加一个模糊查询,在控制器中找到Index方法,修改如下:
public ActionResult Index(string searchString)
{ var movies = from m in db.Movies select m; if (!string.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
return View(movies);
}
执行完
var movies = from m in db.Movies select m;
这句代码之后,我们侦听到movies执行的SQL语句为:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Title] AS [Title],
[Extent1].[ReleaseDate] AS [ReleaseDate],
[Extent1].[Genre] AS [Genre],
[Extent1].[Price] AS [Price]
FROM [dbo].[Movies] AS [Extent1]
执行完这句之后 movies的SQL语句为:
SELECT
[Extent1].[ID] AS [ID],
[Extent1].[Title] AS [Title],
[Extent1].[ReleaseDate] AS [ReleaseDate],
[Extent1].[Genre] AS [Genre],
[Extent1].[Price] AS [Price]
FROM [dbo].[Movies] AS [Extent1]
WHERE [Extent1].[Title] LIKE @p__linq__0 ESCAPE N'~'
可以看得出来,Contains方法,生成了Like后面的语句。