Entity Framework search as case sensitive

Asked

Viewed 353 times

1

People I am using Entity Framework 6, Mysql, ASP.NET MVC.

The problem is this: when I do a search through the form (my search view) EF is case sensitive. When I do the query directly in Mysql, using Sqlyog for example, it makes no difference if I search upper or lower case, then I believe it is something with EF. Someone can give a help?

This is my code that receives the parameter (search text) and query in the database:

  public dynamic GetAll(Func<TEntity, bool> predicate, int limit, ClassContexto contexto)
    {
        dynamic data = null;
        try
        {

            data = limit == 0 ?
                (from p in contexto.Set<TEntity>() select p).Where(predicate).ToList() :
                (from p in contexto.Set<TEntity>() select p).Where(predicate).ToList().Take(limit);

        }
        catch (DbEntityValidationException e)
        {
            data = e.EntityValidationErrors;

            foreach (var eve in e.EntityValidationErrors)
            {
                var x = eve.Entry.Entity.GetType().Name + " - - " + eve.Entry.State;

                foreach (var ve in eve.ValidationErrors)
                {
                    data = ve.PropertyName + "--" + ve.ErrorMessage;
                }
            }
        }
        catch (Exception erro)
        {
            try
            {
                data = erro.InnerException.ToString();
                return data;
            }
            catch
            {
                data = erro.Message.ToString();
                return data;
            }
        }
        finally
        {

        }
        return data;
    }

In the bank, there is an "ANTONIO" record. If I search for "antonio", it is returned null.

How can I change that?

2 answers

3

I didn’t understand your code, but you can solve your problem using the methods String.Toupper or String.Tolower

for example:

var result = colecao.Where(c => c.PropriedadeStr.ToLower().Contains(palavraChave.ToLower()));
  • Eduardo, I used your suggestion and it worked, thank you.

  • cool @alessandremartins . just one thing: when an answer solves your problem, you should mark it as "accept". more details on [tour]

1

Entityframework does the code-level comparison, where in C# "ANTONIO" != "antonio".

I don’t understand what part of your code you are checking, but if you want to compare two strings ignoring uppercase and lowercase letters you can pass the parameter StringComparison.InvariantCultureIgnoreCase in comparison, Example:

String.Equals("antonio", StringComparison.InvariantCultureIgnoreCase);

This will cause the comparison to ignore the difference between lower and upper case letters, however, "antônio" will continue to be different from "antonio", if you really need to take linguistic aspects into account in the comparison you should normalize the two strings before the comparison by removing accents, special characters and etc.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.