2012-06-15 45 views
2

我正在構建類似於facebook的webapp。我想查詢數據庫,所以我得到當前的朋友登錄用戶的職位(從Facebook類似「家」) 確定這裏是EF查詢,從發佈內容的朋友處獲得用戶名

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); 
     } 

的問題是,對於每一個崗位則顯示當前登錄用戶的用戶名,不從F用戶名發佈該帖子的朋友。我需要海報的用戶名(朋友)。 有什麼建議嗎?

回答

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       
    } 

) 
+0

Ty只是帖子,第一個是答案 – Milanix

+0

很高興我可以幫助 – fenix2222

0

不應該在該部分

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

是:

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

除非我misenterpreting,這聽起來像你有什麼說:

加入關於這篇文章的用戶ID的職位表 - >當前用戶 - >朋友 - >朋友id(其循環回來當前用戶)

+0

娜,與此,它顯示從當前用戶 – Milanix