我想从GroupBook Class返回,他们拥有相同组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 :(得分: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();