0

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.

Bronzato
  • 9,438
  • 29
  • 120
  • 212

2 Answers2

1

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

Community
  • 1
  • 1
Totero
  • 2,524
  • 20
  • 34
0

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();
Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Ok so there is no other solution than 'cannonicalize' my street field? If not, then I prefer replacing all my î, ô, ê with i, o, e before saving to DB. – Bronzato May 22 '12 at 13:09
  • @Bronzato: That's just a more limited form of canonicalizing. If you do that without introducing a new column then the data will be visibly different from what the user previously entered, which is something to be avoided IMHO. – Jon May 22 '12 at 13:12
  • Yes but this is not really a problem in my project. Can you show me an example of canonicalize function as a starting point for me? Let's say for replacing all î, ô, ê by i, o, e ? Thanks. – Bronzato May 22 '12 at 13:19
  • @Bronzato: I already gave such a link -- "a good place to start". – Jon May 22 '12 at 13:39
  • Thanks. Your help was precious. – Bronzato May 22 '12 at 15:49