c# - LINQ 多个关键字搜索到 PagedList

标签 c# asp.net-mvc linq pagedlist

我在这里有点迷失,我尝试了几种不同的方法来解决这个问题。到目前为止,我很难编写 LINQ 来执行我想要的操作。

我想获取用户输入字符串,该字符串可以是由空格或“,”分隔的多个关键字。

这里的工作是抓取整个搜索词并将其与帖子中的标题或我可能拥有的任何标签进行比较。我希望用户输入“HTML Preview”,它将与名为“Preview the World”的帖子与标签“HTML”、“CSS”等相匹配...

此查询不起作用...但我正在尝试修改它以使其起作用。

public IPagedList<Post> SearchResultList(string searchTerm, int resultsPerPage, int page)
{
    string[] terms = searchTerm.Split(null);

    TNDbContext context = DataContext;
    return context.Posts
        .Include(a => a.Tags)
        .Include(b => b.Comments)
        .Where(c => (c.Title.Contains(searchTerm) || c.Tags.Any(d => d.Name.StartsWith(searchTerm))) || searchTerm == null)
        .OrderByDescending(x => x.Views)
        .ToPagedList(page, resultsPerPage);

}

我尝试写这个而不是其他“Where”语句

.Where(x => (terms.All(y => x.Title.Contains(y))) || terms == null)

但它一直抛出这个错误

Cannot compare elements of type 'System.String[]'. Only primitive types, enumeration types and entity types are supported.

供引用:

public class Post
{
    public Post()
    {
        Tags = new HashSet<Tag>();
        Comments = new HashSet<Comment>();
    }

    public int Id { get; set; }
    public string Title { get; set; }
    public string UrlTitle { get; set; }
    public DateTime Date { get; set; }

    public DateTime DateEdited { get; set; }

    public string Body { get; set; }

    public string Preview { get; set; }

    public string PhotoPath { get; set; }

    public int Views { get; set; }



    //Navigational

    public ICollection<Tag> Tags { get; set; }

    public ICollection<Comment> Comments { get; set; }

}

public class Tag
{
    public Tag()
    {
        Post = new HashSet<Post>();
    }


    public int Id { get; set; }
    public string Name { get; set; }

    public int TimesTagWasUsed { get; set; }


    //Navigational

    public ICollection<Post> Post { get; set; }


}

最佳答案

您需要从基本查询开始,然后为每个搜索词不断添加 where 子句。试试这个:

TNDbContext context = DataContext;

//Create the base query:
var query = context.Posts
        .Include(a => a.Tags)
        .Include(b => b.Comments)
        .OrderByDescending(x => x.Views);

//Refine this query by adding "where" filters for each search term:
if(!string.IsNullOrWhitespace(searchTerm))
{
    string[] terms = searchTerm.Split(" ,".ToCharArray(),
                                      StringSplitOptions.RemoveEmptyEntries);
    foreach(var x in terms)
    {
        string term = x;
        query = query.Where(post => (post.Title.Contains(term) ||
                                     post.Tags.Any(tag => tag.Name.StartsWith(term))));
    }
}

//Run the final query to get some results:
var result = query.ToPagedList(page, resultsPerPage);

return result;

关于c# - LINQ 多个关键字搜索到 PagedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28442436/

相关文章:

c# - 如何使用 LINQ 从通用列表中获取下一个合适的值?

c# - 有没有一种简单的方法来附加 lambda 并重用 lambda 名称以创建我的 Linq where 条件?

javascript - 将复杂对象从 View 发送到 MVC 操作作为非 ajax 调用

asp.net-mvc - 如何为除/api 路由之外的所有内容设置 IIS 重写规则

c# - 使用 lambda 函数过滤掉重复的(基于所选列)查询结果

c# - Linux 上 .NET Core 的字符编码错误

asp.net-mvc - EF4、MVC 3、Azure 和 Code First 或传统

c# - 按列表中的多个项目进行分组,然后求和并平均 C#

c# - 以编程方式通过路由/模型绑定(bind)传递 URL

c# - WPF 双向绑定(bind) XML