我有以下项目模型:
public class Item
 {
   public int Id {get; set;}
   public string name {get; set;}
   public virtual ICollection<Comment> Comments {get; set;}
   public virtual Comment Comment {get; set;}
 }
我有以下用户模型:
public class User
 {
   public int Id {get; set;}
   public string UserName {get; set;}
   public string email {get; set;}
 }
我有以下评论模型:
public class Comment
 {
   public int Id {get; set;}
   public string text {get; set;}
   public DateTime DateCreated {get; set;}
   public int ItemId {get; set;}
   [ForeignKey("ItemId")]
   public virtual Item Item {get; set;}
   public int UserId {get; set;}
   [ForeignKey("UserId")]
   public virtual User User {get; set;}
 }
在我的 onModelCreating 上下文中,我有
 modelBuilder.Enitity<Item>().HasOptional(c=>c.Comment).WithMany();
我的目标是返回查看只有请求用户评论过的项目。
到目前为止,我已经完成了:
 int UserId = db.Users.Where(u=>u.UserName.Equals(User.Identity.Name))
 .Select(u=>u.Id).FirstOrDefault();
 var itemsWithComments = db.Items.Include(c=>c.Comments).......
此时我想说:选择UserId == Comments.UserId的项目,将Items作为列表返回。
我的观点(使用 Razor)
 @model IEnumerable <project.Models,Item>
 @foreach (var item in Model)
 .....
 .....
任何帮助深表感谢。
如果您需要我澄清任何一点,请询问。
亲切的问候