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?
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.
CONTAINSSTRING is not case-sensitive, but it is accent-sensitive. CONTAINSSTRINGEXACT: Returns TRUE if one text string contains another text string.
In computers, case sensitivity defines whether uppercase and lowercase letters are treated as distinct (case-sensitive) or equivalent (case-insensitive).
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.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With