我正在尝试实现一种方法,其中存储在活动数据库中的关键字(用逗号分隔)与用逗号分隔的给定字符串匹配.
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(); }