c# – LINQ to Entities无法识别方法’System.String [] Split(Char [])’方法,

前端之家收集整理的这篇文章主要介绍了c# – LINQ to Entities无法识别方法’System.String [] Split(Char [])’方法,前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试实现一种方法,其中存储在活动数据库中的关键字(用逗号分隔)与用逗号分隔的给定字符串匹配.
public List<TblActivities> SearchByMultipleKeyword(string keywords)
{
    string[] keyword = keywords.Split(',');
    var results  = (from a in Entities.TblActivities
                    where a.Keywords.Split(',').Any(p => keyword.Contains(p))
                    select a).ToList();
    return results;
}

我收到以下错误

LINQ to Entities does not recognize the method 'System.String[] Split(Char[])' method,and this method cannot be translated into a store expression.

解决方法

对于不涉及太多关键字和太多行的查询,您可以实现这种简单快速解决方案.您可以通过反复优化结果轻松绕过Split功能,如下所示:
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();
 }

猜你在找的C#相关文章