Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 572
Matthew Avatar asked Feb 02 '13 20:02

Matthew


People also ask

Why is SQL case-insensitive?

Keywords in SQL are case-insensitive for the most popular DBMSs. The computer doesn't care whether you write SELECT , select, or sELeCt ; so, in theory, you can write however you like.

Is string contain case-sensitive?

CONTAINSSTRING is not case-sensitive, but it is accent-sensitive. CONTAINSSTRINGEXACT: Returns TRUE if one text string contains another text string.

What is a case-insensitive string?

In computers, case sensitivity defines whether uppercase and lowercase letters are treated as distinct (case-sensitive) or equivalent (case-insensitive).

Is contains case-sensitive in SQL?

SQL Server is, by default, case insensitive; however, it is possible to create a case-sensitive SQL Server database and even to make specific table columns case sensitive. The way to determine if a database or database object is to check its "COLLATION" property and look for "CI" or "CS" in the result.


1 Answers

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/

like image 147
Artless Avatar answered Nov 02 '22 23:11

Artless