Matthew
Matthew

Reputation: 29206

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?

Upvotes: 5

Views: 636

Answers (1)

Artless
Artless

Reputation: 4568

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/

Upvotes: 15

Related Questions