LINQ 包含字符串数组中的一个匹配项

发布于 2024-12-03 19:37:24 字数 1116 浏览 0 评论 0原文

无法使其正常工作:

    /// <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过度放纵 2024-12-10 19:37:24

好吧,经过足够多的努力后,我意识到问题不在于 Any 或 Contains。 Linq to SQL 不喜欢将本地序列(单词)与 SQL 集合 (db.tblSearches) 组合起来。因此,为了完成此任务,您必须将其分解为两个单独的查询。

public static string[] getSearchSuggestions(int SectionID, string Query)
{
    string[] Suggestions;
    string[] Words = Query.Split(' ');

    using (MainContext db = new MainContext())
    {
        string[] all = (from c in db.tblSearches
                    where c.SectionID == SectionID
                    select c.Term).ToArray();

        Suggestions = (from a in all
                       from w in Words
                       where a.Contains(w)
                       select a).Distinct().ToArray();


    }

    return Suggestions;
}

请记住,在第二个查询中,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.

public static string[] getSearchSuggestions(int SectionID, string Query)
{
    string[] Suggestions;
    string[] Words = Query.Split(' ');

    using (MainContext db = new MainContext())
    {
        string[] all = (from c in db.tblSearches
                    where c.SectionID == SectionID
                    select c.Term).ToArray();

        Suggestions = (from a in all
                       from w in Words
                       where a.Contains(w)
                       select a).Distinct().ToArray();


    }

    return Suggestions;
}

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.

流心雨 2024-12-10 19:37:24

首先将数组转换为列表,

 List<string> wordList = Words.ToList();

然后按如下方式更改 linq 查询:

    Suggestions = (from c in db.tblSearches                           
where c.SectionID == SectionID &&                           
Words.Contains(c.Term)                           
select c.Term).ToArray();  

不过,我想我看到了您的问题。在原始查询中,您使用的是 c.Term.Contains()... Contains 是一种扩展方法,需要在实现 Enumerable 的对象上调用,因此您无法在从数据库调用返回的字段上调用它。这就是为什么回答您问题的其他用户说您需要在 Contains 中翻转内容,因为它永远不会让您在 c.Terms 上进行该调用。

First convert your array to a list,

 List<string> wordList = Words.ToList();

then change your linq query as follows:

    Suggestions = (from c in db.tblSearches                           
where c.SectionID == SectionID &&                           
Words.Contains(c.Term)                           
select c.Term).ToArray();  

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.

爱情眠于流年 2024-12-10 19:37:24

您需要翻转 Contains 子句的顺序:

        Suggestions = (from c in db.tblSearches 
                    where c.SectionID == SectionID && 
                    Words.Contains(w => c.Term) 
                    select c.Term).ToArray(); 

You need to flip your order of the Contains clause:

        Suggestions = (from c in db.tblSearches 
                    where c.SectionID == SectionID && 
                    Words.Contains(w => c.Term) 
                    select c.Term).ToArray(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文