Asp.net 选择我关注的用户的帖子
我有两种型号:Asp.net 选择我关注的用户的帖子,asp.net,asp.net-mvc,linq,Asp.net,Asp.net Mvc,Linq,我有两种型号: public class UserProfile { public int Id { get; set; } public virtual ICollection<UserProfile> Following { get; set; } public virtual ICollection<Post> Posts { get; set; } } public class Post { public int Id {
public class UserProfile {
public int Id { get; set; }
public virtual ICollection<UserProfile> Following { get; set; }
public virtual ICollection<Post> Posts { get; set; }
}
public class Post {
public int Id { get; set; }
public virtual UserProfile { get; set; }
}
您可以使用导航属性获得结果:
var result=CurrentUser.UserProfile.Following.SelectMany(u=>u.Posts);
我认为问题在于您试图将查询投影到实体类(Post
)。在EF中,只能将查询投影到匿名类型或DTO(自定义类)
按照你的想法做的另一种方法是:
var following = CurrentUser.UserProfile.Following.Select(u=>u.Id);
var posts = (from p in db.Posts
where following.Contains(p.UserProfile.Id)
select p).ToList();
Contains
扩展方法在SQL中被转换为错误是什么?上面的代码第一个代码缺少一些内容。你能展示一下实际的数据库图表吗?
var following = CurrentUser.UserProfile.Following.Select(u=>u.Id);
var posts = (from p in db.Posts
where following.Contains(p.UserProfile.Id)
select p).ToList();