【问题标题】:How to retrive list of posts when searching in tags table [ASP MVC]在标签表中搜索时如何检索帖子列表 [ASP MVC]
【发布时间】:2016-02-16 15:11:28
【问题描述】:

我想通过标签搜索帖子,例如,当我在搜索框中写“ASP”时,我将搜索它们具有相同标签的帖子并将它们作为帖子列表返回...

这是我的标签表:

这也是我的书课:

[Table("Book")]
public partial class Book
{
    [Key]
    public int Book_id { get; set; }

    [Required]
    [Display(Name ="User Name")]
    [StringLength(128)]
    public string User_ID { get; set; }

    [Required]
    [Display(Name = "Title Name")]
    [StringLength(70,MinimumLength =3)]
    public string Book_name { get; set; }

    [Display(Name = "Edition")]
    [Range(1, 20)]
    public int Edition { get; set; }

    [Display(Name = "Category Name")]
    public int Category_id { get; set; }

    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]    
    [Display(Name = "Publish Date")]
    public DateTime Publish_date { get; set; }

    [StringLength(50)]
    [Display(Name = "Author Name")]
    public string Author_name { get; set; }


    [StringLength(128)]
    [Display(Name = "Cover Image")]
    public string Book_Image { get; set; }


    [StringLength(128,MinimumLength =8)]
    [Url]
    [Display(Name = "Download Link")]
    public string Download_Link { get; set; }


    [StringLength(250)]
    [Display(Name = "Upload Pdf")]
    public string pdf_file { get; set; }


    [AllowHtml]
    [Column(TypeName = "text")]
    [Display(Name = "Description")]
    public string Book_Description { get; set; }


    public int View_Count { set; get; }

    [StringLength(250,MinimumLength =2,ErrorMessage ="Min Tags Input lenght is 2 char")]
    [Display(Name = "Tags")]
    public virtual string TagsListing { get; set; }


    public virtual IList<Tag> Tags
    { get; set; }
    public virtual AspNetUser AspNetUser { get; set; }

    public virtual Category Category { get; set; }
}

还有标签类:

public class Tag
{
    public virtual int Id
    { get; set; }

    public virtual string Name
    { get; set; }

    public virtual string UrlSlug
    { get; set; }

    public virtual string Description
    { get; set; }

    public virtual IList<Book> Books
    { get; set; }
}

我只是想看看访问者搜索的搜索词是否可以在标签表中找到,并返回与相同标签相关的书籍列表..

谢谢

【问题讨论】:

  • 类似db.Tags.Where(t =&gt; t.Name.Equals(input)).SelectMany(t =&gt; t.Books).Distinct() 的东西?这里的任何人都不可能知道您将使用什么特定代码,因为您没有显示有关您的数据访问代码的任何信息。
  • @David,我更新了我的问题并添加了我的书和标签类

标签: asp.net asp.net-mvc asp.net-mvc-4 razor


【解决方案1】:

使用 LINQ

var searchTerm = "Swift";
var postList = db.Books.Where(s => s.Tags.Any(g => g.Name.Contains(searchTerm))).ToList();

Contains 将执行与 LIKE '%Swift%' 等效的 SQL。你也可以试试StartsWithEndsWithas needed.

【讨论】:

  • TagsBooks 实际上在我的上下文中并不存在,因为我首先使用了代码,当我在我的类中创建多对多关系时,tagsbooks 表仅在 sql 管理中创建,但作为一个类是未找到或不存在
  • 您的实体(书籍和标签)看起来如何?
  • 错误 CS1061 'SmartBookLibraryModel' 不包含 'TagBooks' 的定义,并且找不到接受“SmartBookLibraryModel”类型的第一个参数的扩展方法“TagBooks”(您是否缺少 using 指令或程序集参考?)
  • 我更新了我的问题并添加了书籍和标签类模型
  • 查看我更新的答案。下次确保在第一篇文章中包含相关信息。
猜你喜欢
  • 2019-02-15
  • 1970-01-01
  • 1970-01-01
  • 2021-06-22
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多