LINQ to Entities – Case Insensitive Searching with Contains

asp.netc++linqlinq-to-entities

i am trying to query my resultset like this in linq to entities;

var categoriesList = _catRepo.GetAllCategories();


 filteredCategories = categoriesList.Where(c=> c.CategoryName.Contains("for"));

However, i don't get any result becuase the CategoryName is For(Upper Case) in the database. I have also checked the sql server collation and it is set to _CI_AS. I have no idea how to use contains to filter case insensitive string?
I want basically if someone type like;

 filteredCategories = categoriesList.Where(c=> c.CategoryName.Contains("for"));

OR

filteredCategories = categoriesList.Where(c=> c.CategoryName.Contains("For"));

The result should be the same

Best Answer

Try this

filteredCategories = categoriesList.Where(c=>
 c.CategoryName.IndexOf("for", StringComparison.OrdinalIgnoreCase) >= 0)

Contains method works as shown below

public bool Contains(string value)
{
   return this.IndexOf(value, StringComparison.Ordinal) >= 0;
}