2

我正在构建类似于 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);
        }

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

4

2 回答 2

2

好的,你可以试试这个:

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                          
    }

)
于 2012-06-15T02:28:46.950 回答
0

这部分不应该

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

是:

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

?

除非我误解了,否则听起来就像您所说的:

在帖子的用户 id --> 当前用户 --> 朋友 --> 朋友 id 上加入帖子表(循环回当前用户)

于 2012-06-15T02:02:06.777 回答