如何将解释的linq查询转换为本地linq查询

本文关键字:查询 linq 转换 解释 | 更新日期: 2024-09-20 21:54:15

下面是LINQSQL的查询?

Regex wordCounter = new Regex (@"'b('w|[-'])+'b");
IEnumerable<MedicalArticle> sqlQuery = dataContext.MedicalArticles
.Where (article => article.Topic == "influenza");
IEnumerable<MedicalArticle> localQuery = sqlQuery
.Where (article => wordCounter.Matches (article.Abstract).Count < 100);

由于SQL Server不支持正则表达式,因此发生异常。如何将IQueryable<T>转换为IEnumerable<T>

提前感谢

如何将解释的linq查询转换为本地linq查询

您需要在SQL server上运行查询,以列表形式获取结果,然后运行RegEx。我添加了一个".ToList()".

var sqlResults = dataContext.MedicalArticles
.Where (article => article.Topic == "influenza").ToList();
Regex wordCounter = new Regex (@"'b('w|[-'])+'b");
IEnumerable<MedicalArticle> localQuery = sqlResults 
.Where (article => wordCounter.Matches (article.Abstract).Count < 100);

如果这是生产代码,我会考虑使用"LIKE"运算符手工编写SQL,而不是在客户端使用Regex。