C#学习教程:LINQ to Entities无法识别方法’System.String Split(Char )’方法
作者:互联网
LINQ to Entities 不识别方法“System.String ToXX()”,因此该方法无法转换为存储表达式
这是因为LINQ语句最后都是要转为sql语句来执行的,当它转换后,发现sql语句中要执行的方法“XX”,并不是一个在数据库中的存储过程函数,也无法进行转换为存储表达式。
Entity Framework不支持String.Split 。 这只是因为SQL中没有等价物。
public List 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.
绕过
public List 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(); }
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
绕过
public List 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; }
源文:
C#学习教程:LINQ to Entities无法识别方法’System.String Split(Char )’方法,分享-猴子技术宅 (ssfiction.com)
标签:String,识别方法,results,LINQ,Char,Entities,Split,keywords,string 来源: https://www.cnblogs.com/shy1766IT/p/16248083.html