我在这里有点迷失,我尝试了几种不同的方法来解决这个问题。到目前为止,我很难编写 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/