【发布时间】:2013-05-07 10:16:16
【问题描述】:
我有以下物品型号:
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)
.....
.....
非常感谢任何帮助。
如果您需要我澄清任何一点,请询问。
亲切的问候
【问题讨论】:
-
你的意思是
.Where(c=> c.UserId == UserId)?
标签: c# entity-framework lambda