My Software Notes

Useful things I discover

Entity Framework: Contains() (aka ‘Like’) is case insensitive for SQL Server queries

with one comment

Just a note to self.

The Contains method on String when used in an Entity Framework “where” clause is case insensitive.

Example:

Assume the “NoteContent” column on the table “Notes” is a varchar column. If I run the following Linq query against the database where db is a DbContext then I get the same result every time.

var num = db.Notes.Where(n => n.NoteContent.Contains(“NoTe”)).Count();

var num = db.Notes.Where(n => n.NoteContent.Contains(“NOTE”)).Count();

var num = db.Notes.Where(n => n.NoteContent.Contains(“note”)).Count();

All produce the same value for “num”.

I’m making this note to self because I keep forgetting and because I keep seeing people do this:

var someString = “whatever”;

var num = db.Notes.Where(n => n.NoteContent.ToUpper().Contains(someString.ToUpper())).Count();

And, to quote Led Zepplin, “It makes me wonder.”

Advertisements

Written by gsdwriter

December 2, 2014 at 9:53 am

Posted in .NET, Database, LINQ

One Response

Subscribe to comments with RSS.

  1. Need CaseSensitiveeeee ! 🙂

    psygnosisxxx

    August 7, 2015 at 7:26 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: