LINQ查询社交网络:如何执行朋友提要?

时间:2011-12-05 15:34:19

标签: linq asp.net-mvc-3 entity-framework c#-4.0

我有一个社交网站,我遇到一些困难,可以过滤谁可以看到用户发布的更新。

在Facebook中,即使我没有发布任何内容,我也能看到朋友发来的所有帖子。 我有这些表:

DiaryPosts表:

--------------------------------------
| ID | UserID | Content | UpdateTime |
--------------------------------------

朋友表:

--------------------------
| ID | UserID | FriendID |
--------------------------

关注者表:

----------------------------
| ID | UserID | FollowerID |
----------------------------

我有这个查询,现在它无法过滤任何东西:

var diaryPosts = (from d in db.DiaryPosts
                  orderby d.ID descending
                  select new DiaryPostsSet
                  {
                      PostID = d.ID,
                      Author = db.User.Where(m => m.ID == d.UserID).FirstOrDefault().Nickname,
                      Thumbnail = db.User.Where(m => m.ID == d.UserID).FirstOrDefault().Thumbnail,
                      AuthorComment = d.Content, 
                      UserID = d.UserID,
                      Time = d.UpdateTime }).Take(6).ToList();

我试着编写一个where子句,但它没有用。 您对如何撰写此查询有任何建议吗?

4 个答案:

答案 0 :(得分:1)

        var diaryPosts = (from d in db.DiaryPosts
let friendsId = d.Friends.Select(f=>f.FriendID)
 where d.UserID == currentUserId || friendsId.Any(d.UserID)
                          orderby d.ID descending
                          select new DiaryPostsSet
                          {
                              PostID = d.ID,
                              Author = d.Author.Nickname,
                              Thumbnail = d.Author.Thumbnail,
                              AuthorComment = d.Content, 
                              UserID = d.UserID,
                              Time = d.UpdateTime 
                          }).Take(6).ToList();

我认为你需要这样的东西,plz检查语法,如果你愿意,你可以提出一个联接而不是让。

答案 1 :(得分:0)

你尝试了什么失败了?您可以尝试在动态选择查询之外移动where子句:

var diaryPosts = (from d in db.DiaryPosts
              where d.UserID==2 && d.SomethingElse=="someValue"
              orderby d.ID descending
              select new DiaryPostsSet
              {
                ....

这将确保您在进入动态选择之前过滤结果。

答案 2 :(得分:0)

您应该在数据库和数据层(在实体图中)设置关系。如果是这样,您应该通过使用导航属性而不是使用递归数据库调用来依赖这些关系。考虑一下:对于DiaryPosts表中的每个项目,您将对数据库进行2次调用。所以,6个帖子= 13个总数据库调用(1个用于获取帖子,然后6x2用于获取用户)。

您的查询应如下所示:

var diaryPosts = (from d in db.DiaryPosts
                  orderby d.ID descending
                  select new DiaryPostsSet
                  {
                      PostID = d.ID,
                      Author = d.Author.Nickname,
                      Thumbnail = d.Author.Thumbnail,
                      AuthorComment = d.Content, 
                      UserID = d.UserID,
                      Time = d.UpdateTime }).Take(6).ToList();

答案 3 :(得分:0)

答案是:

//queries for diary Posts
        var myDiaryPosts = (from d in db.DiaryPosts
                              join e in db.EstadosDeAlma
                              on d.EstadosDeAlmaID equals e.ID
                              join u in db.User
                              on d.UserID equals u.ID
                              where d.UserID == userset.ID
                              select new DiaryPostsSet { 
                                  PostID = d.ID,
                                  EstadoDeAlmaID = e.ID,
                                  EstadoDeAlma = e.Title,
                                  Author = u.Nickname,
                                  Thumbnail = u.Thumbnail,
                                  UserID = u.ID,
                                  IsDuplicated = d.IsDuplicated,
                                  FriendID = d.FriendID,
                                  FriendName = u.Nickname,
                                  Time = d.UpdateTime,
                                  MessagesCount = d.FriendMessages.Count(m => m.DiaryPostsID == d.ID)
                              });

        var friendsPosts = (from d in db.DiaryPosts
                               join e in db.EstadosDeAlma
                               on d.EstadosDeAlmaID equals e.ID
                               join fr in db.Friends
                               on d.UserID equals fr.FriendID
                               where fr.UserID == userset.ID
                               join u in db.User
                               on fr.FriendID equals u.ID
                               select new DiaryPostsSet
                               {
                                   PostID = d.ID,
                                   EstadoDeAlmaID = e.ID,
                                   EstadoDeAlma = e.Title,
                                   Author = u.Nickname,
                                   Thumbnail = u.Thumbnail,
                                   UserID = u.ID,
                                   IsDuplicated = d.IsDuplicated,
                                   FriendID = d.FriendID,
                                   FriendName = u.Nickname,
                                   Time = d.UpdateTime,
                                   MessagesCount = d.FriendMessages.Count(m => m.DiaryPostsID == d.ID)
                               });

        var followingsPosts = (from d in db.DiaryPosts
                                 join e in db.EstadosDeAlma
                                 on d.EstadosDeAlmaID equals e.ID
                                 join fl in db.Followers
                                 on d.UserID equals fl.UserID
                                 where fl.FollowerID == userset.ID
                                 join u in db.User
                                 on fl.UserID equals u.ID
                                 select new DiaryPostsSet
                                 {
                                     PostID = d.ID,
                                     EstadoDeAlmaID = e.ID,
                                     EstadoDeAlma = e.Title,
                                     Author = u.Nickname,
                                     Thumbnail = u.Thumbnail,
                                     UserID = u.ID,
                                     IsDuplicated = d.IsDuplicated,
                                     FriendID = d.FriendID,
                                     FriendName = u.Nickname,
                                     Time = d.UpdateTime,
                                     MessagesCount = d.FriendMessages.Count(m => m.DiaryPostsID == d.ID)
                                 });

        var diaryPosts = myDiaryPosts.Union(friendsPosts).Union(followingsPosts).OrderByDescending(d => d.Time).Take(6).ToList();