Search in the Entity Framework as case sensitive

1

I am using Entity Framework 6, MySQL, ASP.NET MVC.

The problem is this: when I search the form (my search view), the EF is differentiating capital from lowercase. When I query directly in MySQL, using SqlYog for example, it does not make any difference if I search in uppercase or lowercase, so I think it's something with EF. Can someone give a help ?

This is my code that receives the parameter (search text) and queries 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 you search for "antonio", null is returned.

How can I change this?

    
asked by anonymous 25.06.2016 / 15:56

2 answers

3

I did not understand your code, but you can solve your problem by using the String.ToUpper or String.ToLower

For example:

var result = colecao.Where(c => c.PropriedadeStr.ToLower().Contains(palavraChave.ToLower()));
    
25.06.2016 / 21:52
1

The EntityFramework compares at the code level, where in C # "ANTONIO" != "antonio" .

I do not 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 StringComparison.InvariantCultureIgnoreCase parameter in the comparison,

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

This will cause the comparison to ignore the difference between lowercase and uppercase, but "antônio" will still be different from "antonio" , if you really need to take linguistic aspects into account, you should normalize the two strings before of the comparison by removing accents, special characters and etc.

    
25.06.2016 / 22:23