简体   繁体   English

LINQ to Entities搜索多个关键字的文本属性

[英]LINQ to Entities Searching text properties for multiple keywords

For a website I'm doing we're using LINQ to Entities. 对于我正在做的网站,我们正在使用LINQ to Entities。 I have been charged with adding search functionality to the site. 我被指控为网站添加搜索功能。 I'm trying to figure out the most elegant way to search for multiple keywords (user entered) on a single field in the database. 我试图找出在数据库中的单个字段上搜索多个关键字(用户输入)的最优雅方式。 Allow me to give an example. 请允许我举个例子。

Table columns: 表列:

Name, Description

Example row: 示例行:

"Cookie monster", "Fluffy, likes cookies and blue"

User search (delimiter doesn't matter): 用户搜索(分隔符无关紧要):

"blue fluffy" 

Currently I am using the following: 目前我正在使用以下内容:

    public List<SesameCharacters> SearchByKeywords(string keywords)
    {
        List<SesameCharacters> output = new List<SesameCharacters>();
        string[] k = keywords.ToLower().Split(' ');
        using (SesameStreet_Entities entities = new SesameStreet_Entities())
        {
            IQueryable<SesameCharacters> filter = entities.SesameCharacters;

            foreach (string keyword in k)
                filter = ForceFilter(filter, keyword);

            output = filter.ToList();
        }
        return output;
    }

    private IQueryable<SesameCharacters> ForceFilter(IQueryable<SesameCharacters> filter, string keyword)
    {
        return filter.Where(p => p.Description.ToLower().Contains(keyword));
    }

This currently works as expected but I imagine it is not the best solution to the problem. 这目前按预期工作,但我认为这不是问题的最佳解决方案。 Am I missing something glaringly obvious? 我错过了一些明显的东西吗?

NOTE: This is AND matching. 注意:这是AND匹配。

I found this worked for me - this is using VB.Net with Entity Framework 4.0, but I'm sure the principle translates. 我发现这对我有用 - 这是使用VB.Net与Entity Framework 4.0,但我确信原理翻译。

This one does the "OR" style query: 这个执行“OR”样式查询:

    Function Search(ByVal query As String) As IQueryable(Of Product)
    Dim queryWords As String() = query.Split()
    Dim entities As New Entities()

    Return entities.Products.Where(Function(p) queryWords.Any(Function(w) p.Description.Contains(w)))
End Function

And this one does "AND" style queries: 这个人做“和”风格的查询:

Function Search(ByVal query As String) As IQueryable(Of product)
    Dim queryWords As String() = query.Split()
    Dim entities As New Entities()

    Return entities.Products.Where(Function(p) queryWords.All(Function(w) p.Description.Contains(w)))
End Function

Looks like Linq to Entities doesn't support contains: 看起来Linq to Entities不支持包含:

http://msdn.microsoft.com/en-us/library/bb738638.aspx http://msdn.microsoft.com/en-us/library/bb738638.aspx

I'd roll my own query for this one. 我会为这个推出自己的查询。 Your probably going to want full control over the text search queries if you find out these types of searches become performance issues. 如果您发现这些类型的搜索会成为性能问题,您可能希望完全控制文本搜索查询。

how about instead of: 怎么样而不是:

IQueryable<SesameCharacters> filter = entities.SesameCharacters;

        foreach (string keyword in k)
            filter = ForceFilter(filter, keyword);

        output = filter.ToList();

Do: 做:

return (from c in entities.SesameCharacters
         where k.Contains(c..Description.ToLower())
         select c
         ).ToList();

Not really LINQ related but you could consider using SQL Server Full-Text Search while CONTAINS predicate understands boolean operators—AND, OR, AND NOT. 不是LINQ相关,但您可以考虑使用SQL Server 全文搜索,CONTAINS谓词理解布尔运算符-AND ,OR和AND NOT。

This article could also be useful: Dynamically Composing Expression Predicates 本文也很有用: 动态编写表达式谓词

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM