3

Suppose I have a database of Khans. I can use this entity framework code to fetch a khan by name

Khan khan=context.Khans.Where(x=>x.Name.Contains("Öge")).Single();

The problem is that in practice users will type "Oge" instead of "Öge" and won't get any matches. Is there an easy way to deal with this so that letters with accent marks always count as letters without accents for searches?

I found this SO question that has a solution for the Compare method but I can't seem to use this for a database query:

string.Compare(s1, s2, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace);
Community
  • 1
  • 1
Matthew
  • 4,149
  • 2
  • 26
  • 53
  • 1
    One strategy would be to create the set of variants and then use all of them in the query. – juharr Sep 16 '16 at 17:43
  • 1
    Possible duplicate of [LINQ Where Ignore Accentuation and Case](http://stackoverflow.com/questions/7418624/linq-where-ignore-accentuation-and-case) – Cᴏʀʏ Sep 16 '16 at 17:46
  • @Cᴏʀʏ thiscomment should be part of your answer, also with juharr's methode, the OP has now as 3 possibles answers to his question. He may choose what he feels is best. – Antoine Pelletier Sep 16 '16 at 17:56
  • @AntoinePelletier: My comment was auto-added as a result of my close-as-duplicate vote. I don't think adding it to my answer will be of any benefit once this question does eventually get closed. – Cᴏʀʏ Sep 16 '16 at 18:02

1 Answers1

6

You may need to change the collation of the field in the database table to be case- and accent-insensitive for LINQ to work with standard methods like .Contains():

ALTER TABLE Khans ALTER COLUMN Name [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AI

The "CI" at the end of the collation name means "case-insensitive" and the "AI" means "accent-insensitive."

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194