文章出處

前言:

* 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后面的語句。

 

文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()