Search code examples
entity-frameworkasp.net-mvc-5linq-to-entities

EF Linq search results while ignoring special characters


I'm trying to create a LINQ method query where a user can input text for search criteria which will search a table column and ignore special characters when looking for the results.

For example: Inputting "ab cd" would result in returning both "ab cd" and "ab, cd".

My LINQ Method is currently the following:

public List<type> GetData(string input)
{ 
    var results = dbContext.table
                  .Where(s => s.Column.Contains(input))
                  .ToList();
    return results;
}

Solution

  • You can use DbFunctions.Like as of EF 6.2:

    public List<type> GetData(string input)
    { 
        var results = dbContext.table
                      .Where(s => DbFunctions.Like(s.Column, "%"+input.Replace(' ', '%')+"%"))
                      .ToList();
        return results;
    }