LINQ 包含字符串数组中的一个匹配项
无法使其正常工作:
/// <summary>
/// Retrieve search suggestions from previous searches
/// </summary>
public static string[] getSearchSuggestions(int SectionID, string Query)
{
string[] Suggestions;
string[] Words = Query.Split(' ');
using (MainContext db = new MainContext())
{
Suggestions = (from c in db.tblSearches
where c.SectionID == SectionID &&
Words.Any(w => c.Term.Contains(w))
select c.Term).ToArray();
}
return Suggestions;
}
我得到:
System.NotSupportedException:本地序列不能在查询运算符(Contains 运算符除外)的 LINQ to SQL 实现中使用。
我想返回字段 c.Term
包含 Words
数组中任何单词的记录。我也想按比赛总数排序,但这似乎很难做到!我找到了此 MSDN。但我也无法让它与我的查询一起工作。另外发现了这个,但它不起作用。
Having trouble getting this to work:
/// <summary>
/// Retrieve search suggestions from previous searches
/// </summary>
public static string[] getSearchSuggestions(int SectionID, string Query)
{
string[] Suggestions;
string[] Words = Query.Split(' ');
using (MainContext db = new MainContext())
{
Suggestions = (from c in db.tblSearches
where c.SectionID == SectionID &&
Words.Any(w => c.Term.Contains(w))
select c.Term).ToArray();
}
return Suggestions;
}
I get:
System.NotSupportedException: Local sequence cannot be used in LINQ to SQL implementations of query operators except the Contains operator.
I want to return records where the field c.Term
contains any of the words in the Words
array. I would also like to have it ordered by the total number of matches, but that seems really hard to do! I found this MSDN. But I can't get it to work with my query either. Also found this but it's not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,经过足够多的努力后,我意识到问题不在于 Any 或 Contains。 Linq to SQL 不喜欢将本地序列(单词)与 SQL 集合 (db.tblSearches) 组合起来。因此,为了完成此任务,您必须将其分解为两个单独的查询。
请记住,在第二个查询中,
Contains
区分大小写,因此您可能必须添加不区分大小写的扩展方法,或者使用老方法并踢掉它们.ToUpper()< /代码>。我在 4.0 中在我的一个上下文中运行了这个,它正确返回了所有 88 个字符串(可能是 9814 个)。虽然这是一个彻底的PITA。在这个问题上肯定+1。
编辑:
将
.Distinct()
添加到最终答案中。Ok, after plugging away enough at it I realized that the problem wasn't the Any or the Contains. Linq to SQL doesn't like you combining the local sequence (words) with the SQL collection (db.tblSearches). So in order to accomplish this, you have to break it out into 2 separate queries.
Keep in mind, that in the second query, the
Contains
is case sensitive, so you might have to add a case-insensitive extension method or go old school and kick them.ToUpper()
. I ran this in 4.0 on one of my contexts and it returned all 88 strings correctly (out of a possible 9814). Though it was a thorough PITA. Definite +1 on this question.Edit:
Added
.Distinct()
to the final answer.首先将数组转换为列表,
然后按如下方式更改 linq 查询:
不过,我想我看到了您的问题。在原始查询中,您使用的是 c.Term.Contains()... Contains 是一种扩展方法,需要在实现 Enumerable 的对象上调用,因此您无法在从数据库调用返回的字段上调用它。这就是为什么回答您问题的其他用户说您需要在 Contains 中翻转内容,因为它永远不会让您在 c.Terms 上进行该调用。
First convert your array to a list,
then change your linq query as follows:
I think I see your problem though. In your original query you are using c.Term.Contains()... Contains is an extension method that needs to be called on an object that implements Enumerable so you cannot call it on a field you are getting back from your database call. That is why the other user that answered your question said you needed to flip things around in your Contains, because it is never going to let you make that call on c.Terms.
您需要翻转 Contains 子句的顺序:
You need to flip your order of the Contains clause: