简体   繁体   中英

Why is String.Contains case-insensitive in this query?

I'm working my way through this ASP MVC tutorial . This page of the tutorial deals with writing a simple "search" page. The controller contains this method:

public ActionResult SearchIndex(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); 
}

According to MSDN, String.Contains is case-sensitive. But when I navigate to [website url]/Movies/SearchIndex?searchString=mel , it returns a movie with the title Melancholia as a result. If I inspect the controller method in the debugger, searchString is mel (in lower case) as expected.

Why does String.Contains match this title case-insensitively?

When using Linq to entities the query is done by the SQL Server. Your Lambda expression is translated to an SQL query, so whether or not it is case sensitive depends on the server configuration.

If you'd like to change your SQL Server collation and make it case sensitive, please see this page: http://blog.sqlauthority.com/2007/04/30/case-sensitive-sql-query-search/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM