C# – LINQ to Entities does not recognize the method ‘Int32 IndexOf(System.String, System.StringComparison)’ method

centity-frameworklinqnet

I have executed a linq query by using Entityframework like below

GroupMaster getGroup = null;
getGroup = DataContext.Groups.FirstOrDefault(item => keyword.IndexOf(item.Keywords,StringComparison.OrdinalIgnoreCase)>=0 && item.IsEnabled)

when executing this method I got exception like below

LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String, System.StringComparison)' method, and this
method cannot be translated into a store expression.

Contains() method by default case sensitive so again I need to convert to lower.Is there any method for checking a string match other than the contains method and is there any method to solve the indexOf method issue?

Best Answer

You really only have four options here.

  1. Change the collation of the database globally. This can be done in several ways, a simple google search should reveal them.
  2. Change the collation of individual tables or columns.
  3. Use a stored procedure and specify the COLATE statement on your query
  4. perform a query and return a large set of results, then filter in memory using Linq to Objects.

number 4 is not a good option unless your result set is pretty small. #3 is good if you can't change the database (but you can't use Linq with it).

numbers 1 and 2 are choices you need to make about your data model as a whole, or if you only want to do it on specific fields.

Changing the Servers collation: http://technet.microsoft.com/en-us/library/ms179254.aspx

Changing the Database Collation: http://technet.microsoft.com/en-us/library/ms179254.aspx

Changing the Columns Collation: http://technet.microsoft.com/en-us/library/ms190920(v=sql.105).aspx

Using the Collate statement in a stored proc: http://technet.microsoft.com/en-us/library/ms184391.aspx