LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String, System.StringComparison)' method

.net c# entity-framework linq

Question

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?

1
12
4/22/2014 6:15:15 AM

Accepted 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

7
4/22/2014 6:51:01 AM

Popular Answer

The IndexOf method Of string class will not recognized by Entity Framework, Please replace this function with SQLfunction or Canonical functions

You can also take help from here or maybe here

You can use below code sample:

DataContext.Groups.FirstOrDefault(item => 
    System.Data.Objects.SqlClient.SqlFunctions.CharIndex(item.Keywords, keyword).Value >=0 && item.IsEnabled)


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow