0

What I have read so far that Linq (and C# for that matter) already does case sensitive checks.

How can I make it case in-sensitive?

Here is my code and it returns me 0 rows and where as I do have one record in the database

bool result = Employee.SearchBy(x => x.Name.Contains("johN schulZ"));

and this return true:

bool result = Employee.SearchBy(x => x.Name.ToLower().Contains("johN schulZ".ToLower()));

The latter solution is working, but I would like to know if there may be a more convenient way.

Servy
  • 202,030
  • 26
  • 332
  • 449
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • 1
    What query provider are you using? Or is this LINQ to objects? The query provider you're using will determine how you go about doing a case insensitive compare. – Servy Dec 10 '13 at 19:38
  • 4
    Don't you already have a solution? – LittleSweetSeas Dec 10 '13 at 19:38
  • similar question: http://stackoverflow.com/questions/444798/case-insensitive-containsstring – Rudis Dec 10 '13 at 19:38
  • @LittleSweetSeas: yes but do not want to use `ToLower()` @Servy:Linq to SQL – Nick Kahn Dec 10 '13 at 19:40
  • Do as you're doing or [create your own extension method](http://stackoverflow.com/questions/17563929/how-to-make-string-contains-case-insensitive). You can't do that in LINQ-to-SQL though. – Jeroen Vannevel Dec 10 '13 at 19:40
  • What does this have to do with Linq? The `Contains` expression is where your problem is. edit - oh I see, it's acually Linq to Sql. – asawyer Dec 10 '13 at 19:40
  • Please see: http://stackoverflow.com/questions/841226/case-insensitive-string-compare-in-linq-to-sql – Francis MacDonald Dec 10 '13 at 20:00
  • I downvoted because the question must be incorrectly tagged as Linq2Sql (as a consequence of the answer provided by the OP below). – spender Dec 10 '13 at 23:03

2 Answers2

6

Case sensitivity when using a database (I'm assuming Linq2EF or Linq2Sql) is a function of the database. You should change the collation of the column/database to one that is case insensitive.

See:

http://technet.microsoft.com/en-us/library/ms190920.aspx

or

http://technet.microsoft.com/en-us/library/ms175835.aspx

spender
  • 117,338
  • 33
  • 229
  • 351
0

this works perfectly what I wanted - thanks all.

bool result = Employee.SearchBy(x => x.Name.Equals("johN schulZ", StringComparison.CurrentCultureIgnoreCase));

or

bool result = Employee.SearchBy(x => x.Name.Contains("johN schulZ", StringComparison.CurrentCultureIgnoreCase));
Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • 1
    "Method 'Boolean Equals(System.String, System.String, System.StringComparison)' has no supported translation to SQL.". I'm downvoting your question for misleading us about the use of Linq2Sql. It's clear that actually you are dealing with a local collection, otherwise this would not work. – spender Dec 10 '13 at 23:00