码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET MVC5--Contains

时间:2015-06-20 20:49:15      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

前言:

* 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方法,修改如下:

 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         }

执行完

 var movies = from m in db.Movies select m;

这句代码之后,我们侦听到movies执行的SQL语句为:
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]

执行完这句之后 movies的SQL语句为:

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~

可以看得出来,Contains方法,生成了Like后面的语句。

 

ASP.NET MVC5--Contains

标签:

原文地址:http://www.cnblogs.com/caofangsheng/p/4590974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!