我正在构建类似于 facebook 的 webapp。我想查询数据库,以便从当前登录用户的朋友那里获取帖子(类似于 facebook 中的“家”) 好的,这是模型
public User()
{
this.Friends = new HashSet<Friend>();
this.Friends1 = new HashSet<Friend>();
this.Posts = new HashSet<Post>();
}
public int userID { get; set; }
public System.Guid user_Unique_ID { get; set; }
public string username { get; set; }
public string profilePicUrl { get; set; }
public virtual ICollection<Friend> Friends { get; set; }
public virtual ICollection<Friend> Friends1 { get; set; }
public virtual ICollection<Post> Posts { get; set; }
public partial class Friend
{
public int friendshipID { get; set; }
public int userfrID { get; set; }
public int friendID { get; set; }
public virtual User User { get; set; }
public virtual User User1 { get; set; }
}
public partial class Post
{
public int postID { get; set; }
public int userpostID { get; set; }
public System.DateTime postedOn { get; set; }
public string text { get; set; }
public int likes { get; set; }
public virtual User User { get; set; }
}
我尝试的是,创建一个视图模型:
public class HomePosts
{
public int postID { get; set; }
public int userID { get; set; }
public int userpostID { get; set; }
public string username { get; set; }
public string text { get; set; }
public System.DateTime postedOn { get; set; }
public int likes { get; set; }
public int userfrID { get; set; }
public int friendID { get; set; }
}
这是我的查询:
using (var context = new ASPNETDBEntities())
{
MembershipUser x = Membership.GetUser();
User wad = context.Users.Where(u => u.user_Unique_ID == (System.Guid)x.ProviderUserKey).SingleOrDefault();
int id = wad.userID;
var allposts = (from u in db.Users
join f in db.Friends
on u.userID equals f.userfrID
where f.userfrID == id
join p in db.Posts
on f.friendID equals p.userpostID
select new HomePosts
{
friendID = f.friendID,
likes = p.likes,
postedOn = p.postedOn,
postID = p.postID,
text = p.text,
userfrID = f.userfrID,
userID = u.userID,
username = u.username, <-- this is the problem
userpostID = p.userpostID
}).OrderByDescending(u => u.postedOn).ToList();
return View(allposts);
}
问题是,对于每个帖子,它都显示当前登录用户的用户名,而不是发布该帖子的朋友的用户名。我需要发帖人(朋友)的用户名。有什么建议么 ?