2015-02-10 241 views
2

我有點迷路了,我嘗試了幾種不同的方法來解決它。到目前爲止,我很難寫出LINQ來做我想做的事情。LINQ多個關鍵字搜索PagedList

我想採用用戶輸入字符串,可以是多個關鍵字,可以用空格或「,」分隔。

這裏的工作是抓住整個搜索詞,並將其與Post或任何標籤中的標題進行比較。我希望用戶輸入「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); 

} 

我試着寫的,而不是其他的 「去哪兒」 的語句

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

這一點,但它不斷拋出這個錯誤

無法比擬 'System.String []' 類型的元素。只支持原始類型,枚舉類型和實體類型。

供參考:

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

你需要用鹼查詢開始,然後不斷增加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; 
+0

我修改這個稍微因爲我可能是一點點不清楚。我想用空格或逗號分隔搜索詞。無論是所有的善良的人,我都非常感謝這個人的大腦放屁。 我只是把它留給只用空白符分割,如下所示: string [] terms = searchTerm.Split(null); – Spets 2015-02-11 04:22:13

1

你可以「從」鳥巢報表查詢與額外的,所以這樣的事情應該工作:

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