1

I having and issue with a sql linq query that I have. I have a query that when you type a person name in an input text box I have to show a list of persons that contain that name, but the problem that I am having is the following: a person name can look like so jose ou josé. It's the same name but one with é an the other without é. My query

var person = (from p in context.Person
              where p.Name.Contains(personName) || p.Name.StartsWith(personName) || p.Name.EndsWith(personName)                            
                          select p).OrderBy(m => m.Name).ToPagedList(page, 10);
   return person;

The Idea is to when I wright jose the query get all the people that have jose and josé and vice versa. If I wright jose the query only returns people that have jose in there name and does not allsow return people that have josé with the é. Does any one know how I can ressolve this issue. Thank you

Chico José
  • 121
  • 2
  • 16
  • Btw, `p.Name.Contains(personName)` handles `StartsWith` and `EndsWidth` so no need of them. What about your main question, from the database point of view the names in example are different. – Ivan Stoev Mar 20 '16 at 17:10
  • Hello, The name are the same but one has é an the other does not. I have to return the name that have é or do not have é. Thank – Chico José Mar 20 '16 at 17:27

1 Answers1

0

You could use String.Compare, usage in current culture would look like this (if you do not wish to use your current culture, then you could simply use InvariantCulture instead):

if (String.Compare("Jose", "José", CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace) == 0)
{
    // both strings are equal
}

You would have to implement this check directly into your linq query like this:

var person = (from p in context.Person
              where String.Compare(p.Name, personName, CultureInfo.CurrentCulture, CompareOptions.IgnoreNonSpace) == 0                        
                          select p).OrderBy(m => m.Name).ToPagedList(page, 10);
return person;

This is all very well explained in this answer.

Peroxy
  • 646
  • 8
  • 18