LINQ to Entities无法识别方法'Int32 IndexOf(System.String,System.StringComparison)'方法 [英] LINQ to Entities does not recognize the method 'Int32 IndexOf(System.String, System.StringComparison)' method
问题描述
我已经通过使用如下所示的Entityframework执行了linq查询
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无法识别方法'Int32 IndexOf(System.String,System.StringComparison)'方法,并且 方法不能转换为商店表达式.
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()方法区分大小写,因此再次需要转换为小写.除了contains方法外,是否有任何方法可以检查字符串是否匹配?是否有任何方法可以解决indexOf方法问题?
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?
推荐答案
您这里实际上只有四个选项.
You really only have four options here.
- 全局更改数据库的排序规则.这可以通过多种方式完成,一个简单的Google搜索就可以显示它们.
- 更改单个表或列的排序规则.
- 使用存储过程并在查询中指定COLATE语句
- 执行查询并返回大量结果,然后使用Linq to Objects在内存中进行过滤.
除非您的结果集很小,否则
数字4不是一个好的选择. #3如果您不能更改数据库(但不能与它一起使用Linq)则很好.
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).
数字1和2是您需要对整个数据模型进行选择的选择,或者仅在特定字段上进行选择.
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.
更改服务器排序规则: http://technet.microsoft.com/en-us/library/ms179254.aspx
Changing the Servers collation: http://technet.microsoft.com/en-us/library/ms179254.aspx
更改数据库排序规则: http://technet.microsoft.com/en-us/library/ms179254.aspx
Changing the Database Collation: http://technet.microsoft.com/en-us/library/ms179254.aspx
更改列排序规则: http://technet.microsoft.com/en -us/library/ms190920(v = sql.105).aspx
在存储的proc中使用Collate语句: http://technet.microsoft.com/en-us/library/ms184391.aspx
Using the Collate statement in a stored proc: http://technet.microsoft.com/en-us/library/ms184391.aspx
这篇关于LINQ to Entities无法识别方法'Int32 IndexOf(System.String,System.StringComparison)'方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!