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

后端 未结 4 1789
小蘑菇
小蘑菇 2021-02-19 22:30

I have executed a linq query by using Entityframework like below

GroupMaster getGroup = null;
getGroup = DataContext.Groups.FirstOrDefault(item => keyword.Ind         


        
相关标签:
4条回答
  • 2021-02-19 23:05

    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

    0 讨论(0)
  • 2021-02-19 23:11

    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)
    
    0 讨论(0)
  • 2021-02-19 23:13

    Erik Funkenbush' answer is perfectly valid when looking at it like a database problem. But I get the feeling that you need a better structure for keeping data regarding keywords if you want to traverse them efficiently.

    Note that this answer isn't intended to be better, it is intended to fix the problem in your data model rather than making the environment adapt to the current (apparently flawed, since there is an issue) data model you have.

    My main suggestion, regardless of time constraint (I realize this isn't the easiest fix) would be to add a separate table for the keywords (with a many-to-many relationship with its related classes).

    [GROUPS] * ------- * [KEYWORD]
    

    This should allow for you to search for the keyword, and only then retrieve the items that have that keyword related to it (based on ID rather than a compound string).

    int? keywordID = DataContext.Keywords.Where(x => x.Name == keywordFilter).Select(x => x.Id).FirstOrDefault();
    
    if(keywordID != null)
    {
        getGroup = DataContext.Groups.FirstOrDefault(group => group.Keywords.Any(kw => kw.Id == keywordID));
    }
    

    But I can understand completely if this type of fix is not possible anymore in the current project. I wanted to mention it though, in case anyone in the future stumbles on this question and still has the option for improving the data structure.

    0 讨论(0)
  • 2021-02-19 23:27

    Instead you can use this method below for lowering the cases:

    var lowerCaseItem = item.ToLower();
    

    If your item is of type string. Then this might get you through that exception.

    0 讨论(0)
提交回复
热议问题