Bronzato
Bronzato

Reputation: 9342

Perform a search with LINQ and ignoring characters like î or ô or ê

In my DB I have some texts like this one: 'AVENUE GEORGES LEMAîTRE'. As you can see there is a ^ on the 'i' character.

Is it possible to perform a search with LINQ and ignoring this special character.

So I would like to search for 'AVENUE GEORGES LEMAITRE' and find 'AVENUE GEORGES LEMAîTRE'.

var address = AddressRepository.Find(m => m.Street == "AVENUE GEORGES LEMAITRE").ToList();

Possible? How?

Thanks.

Upvotes: 0

Views: 601

Answers (2)

Totero
Totero

Reputation: 2574

try:

List<String> address = (from ad in addressRepository where string.Compare("AVENUE GEORGES LEMAITRE", ad.Street.ToUpperInvariant(),CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace) == 0 select ad.Street).ToList();

Otherwise

This blog entry might help:

It details how to remove Diacritics from strings.

go to the bottom for the code sample.

Once the Characters are replaced the strings can be compared.

Hope this helps.

http://www.siao2.com/2007/05/14/2629747.aspx

Upvotes: 1

Jon
Jon

Reputation: 437386

You should modify your database model to include a "canonical" version of Street along with the "actual" version that you display to or accept from the user. This canonical version could for example be all uppercase, with all diacritics removed, and normalized to a specific Unicode normalization form.

Assuming that you have a method Canonicalize that does this (this might be a good place to start), you would invoke it when saving an address:

var address = /* incoming data */
address.CanonicalStreet = Canonicalize(address.Street);
/* save the model */

And you would also use it when retrieving data:

var street = "AVENUE GEORGES LEMAITRE";
street = Canonicalize(street);
var address = AddressRepository.Find(m => m.CanonicalStreet == street).ToList();

Upvotes: 0

Related Questions