EF 查询,从发布内容的朋友那里获取用户名

本文关键字:那里 朋友 获取 用户 布内容 查询 EF | 更新日期: 2023-09-27 18:37:10

我正在构建类似于Facebook的Web应用程序。我想查询数据库,以便从当前登录用户的朋友那里获得帖子(类似于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);
        }

问题是,对于每个帖子,它都会显示当前登录用户的用户名,而不是发布该帖子的朋友的用户名。我需要海报(朋友)的用户名。有什么建议吗?

EF 查询,从发布内容的朋友那里获取用户名

好的

,你能试试这个吗:

from f in db.Friends
join u in db.Users
on f.friendID equals u.userID  
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, 
    userpostID = p.userpostID                             
}

或者,如果 EF 设置正确,则可以执行以下操作:

var posts = db.Posts.Where(p => p.User.Any(u => u.Friends.Any(f => f.userfrID == id))).Select(p => new HomePosts 
    {                                 
        // your selections                          
    }
)

这部分不应该

join p in db.Posts    
on f.friendID equals p.userpostID

是:

join p in db.Posts
on u.userID equals p.userpostID

除非我输入错误,否则听起来像你说的:

在帖子的用户 ID 上加入帖子表 -->当前用户 --> 朋友 -

->朋友 ID(循环回当前用户)