LINQ多个关键字搜索到PagedList

时间:2015-02-10 21:45:51

标签: c# asp.net-mvc linq pagedlist

我有点迷失在这里,我尝试了几种不同的方法来解决它。到目前为止,我很难写出LINQ来做我想做的事。

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

这里的工作方法是抓取整个搜索词并将其与帖子中的标题或我可能拥有的任何标签进行比较。我希望用户输入" HTML预览"这将匹配一个名为"预测世界"标签" 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);

}

我试着写这个而不是另一个&#34; Where&#34;声明

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

但它不断抛出此错误

  

无法比较&#39; System.String []&#39;类型的元素。仅支持基本类型,枚举类型和实体类型。

供参考:

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; }


}

2 个答案:

答案 0 :(得分:2)

您需要从基本查询开始,然后为每个搜索字词不断添加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;

答案 1 :(得分:1)

您可以使用其他&#39;来嵌套查询。语句,所以这样的事情应该有效:

var list = (from post in context.Posts.Include(a => a.Tags).Include(b => b.Comments)
            from term in terms
            where post.Title.Contains(term) || post.Tags.Any(d => d.Name.StartsWith(term))
            select post).OrderByDescending(x => x.Views);