LINQ to Entities does not recognize the method 'System.String[] Split(Char[])' method,

前端 未结 6 1975
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 02:17

I am trying to implement a method where the keywords stored in the database for an activity (split by a comma) match the giving string split by a comma.

publ         


        
相关标签:
6条回答
  • 2020-12-04 02:19

    Yes you can do it like that:

    public List<TblActivities> SearchByMultipleKeyword(string keywords)
    {
        string[] keywordsSeparated = keywords.Split(',');
        var results  = (from a in Entities.TblActivities
                        where keywordsSeparated.Any(keyword => a.Keywords.Contains(keyword))
                        select a).ToList();
        return results;
    }
    
    0 讨论(0)
  • 2020-12-04 02:22

    Not sure but you can try: Since the error appears to be looking for an array this might work.

    string[] keyword = keywords.Split(new char[] {','});
    
    var results  = (from a in Entities.TblActivities
                    where a.Keywords.Split(new char[] {','}).Any(p => keyword.Contains(p))
                    select a).ToList();
    
    0 讨论(0)
  • 2020-12-04 02:24

    For queries that do not involve too many keywords and too many rows you could implement this simple and quick solution. You can easily get around the Split function by repeatedly refining your results as follows:

     public List<TblActivities> SearchByMultipleKeyword(string keywords)
     {
         string[] keywords = pKeywords.Split(',');
    
         var results = Entities.TblActivities.AsQueryable();    
    
         foreach(string k in keywords){
    
             results  = from a in results
                        where a.Keywords.Contains(k)
                        select a;
         }
         return results.ToList();
     }
    
    0 讨论(0)
  • 2020-12-04 02:31

    String.Split isn't supported by Entity Framework. This is simply because there is no equivalent in SQL.

    A solution is:

    1. to define a custom function in the database This article proposes several solutions: http://sqlperformance.com/2012/07/t-sql-queries/split-strings
    2. to declare this function usable by LINQ to Entities using the [EdmFunction] attribute like explained here: How to call DB function from EF LINQ query?
    0 讨论(0)
  • 2020-12-04 02:36

    You cannot do this using the Entity Framework, as the error message says.

    However, there are options.

    One option is to realize that, if keywords are stored as A,B,C,D, then x is in there if

    a.Keywords.StartsWith(x + ",") || 
    a.Keywords.Contains("," + x + ",") || 
    a.Keywords.EndsWith("," + x)
    

    That works if x does not contain , itself. The downside is that this will do a full scan of the table, or of an index containing the Keywords column.

    The other option is to normalize your database. After all, you have a one to many relationship between activity and keyword. Then model it as such: in addition to an Activities table (without the Keywords column), have a KeyWords table with two columns, a foreign key to your activities table, and a keyword column. This will allow you add an index on the keyword column, which can make the query super-fast.

    UPDATE

    I reread your question, and noticed that you are not testing for keyword equality, but just Contains. If so, why don't you just do the following?

    a.Keywords.Contains(x)
    
    0 讨论(0)
  • 2020-12-04 02:40

    LINQ to Entities tries to translate your LINQ query into SQL. Since it doesn't know how to do String.Split in a SQL query, it fails.

    This means that unless you want to write a SQL implementation of String.Split, you can only do it in LINQ to objects, which means you need to load all of your data into memory first, then do the where clause. One easy way to do this is using .ToList():

    var results  = (from a in Entities.TblActivities select a).ToList(); //Results are now in memory
    results = results.Where(a =>
         a.Keywords.Split(',').Any(p => keyword.Contains(p))).ToList(); //This uses LINQ-to-objects
    
    0 讨论(0)
提交回复
热议问题