2016-02-28 49 views
1

我想从GroupBook类,A组的书籍,他们有相同的组ID返回...如何从多对多的结果中返回虚拟机?

我有这些表之间的这种关系:

public class GroupBook 
{ 
    [Key] 
    public int Id { get; set; } 

    [Required] 
    public int Book_Id { get; set; } 

    [Required] 
    public string User_Id { get; set; } 

    [Required] 
    public int Group_id { get; set; } 

    public virtual AspNetUser AspNetUser { get; set; } 

    public virtual Book Book { get; set; } 

    public virtual Group Group { get; set; } 

} 

public partial class Group 
{ 
    public Group() 
    { 

     GroupBooks = new HashSet<GroupBook>(); 
    } 
    [Key] 
    public int Group_id { get; set; } 

    [Required] 

    public string Group_name { get; set; } 

    public string Group_description { get; set; } 

    public string UrlSlug { get; set; } 

    public int state { get; set; } 

    [Required]   
    public string User_Id { get; set; } 

    public virtual AspNetUser AspNetUser { get; set; } 

    public virtual ICollection<GroupBook> GroupBooks { get; set; } 

} 

也是我的书表:

public partial class Book 
{ 
    public Book() 
    { 

     GroupBooks = new HashSet<GroupBook>(); 
    } 
    [Key] 
    public int Book_id { get; set; } 
    [Required] 
    [StringLength(128)] 
    public string User_ID { get; set; } 
    public string UrlSlug { get; set; } 
    [Required] 
    public string Book_name { get; set; } 
    public string Book_Description { get; set; } 
    public virtual ICollection<GroupBook> GroupBooks { get; set; } 
    public virtual AspNetUser AspNetUser { get; set; } 
} 

,这里是我的VM:

public class GroupVm 
{ 
     public List<Book> Book { set; get; } 


} 

我要在VM集合这样返回的书:

public ActionResult Index(int? Group_id) 
    {    
     Vm=db.books.where(//What to add here to return set of books that == the group id I will send via method) 
    } 

在前面的例子,我只是想回到帐套具有相同GROUP_ID存在,如何做到这一点,请?

回答

0

DbSet<GroupBook> GroupBooks加入您的DbContext

然后做一些事情:

var vm = new GroupVm(); 
vm.Book = db.GroupBooks.Where(g => g.Group_id == Group_id).Select(g => g.Book).ToList(); 
+0

感谢您的帮助爵士..它的作品:) –