Exemplo n.º 1
0
 public List <Post> GetAllPosts()
 {
     using (IDbConnection cnx = Connection)
     {
         cnx.Open();
         string query = "SELECT * FROM posts JOIN users ON posts.user_id WHERE posts.user_id = users.id; SELECT * FROM likes JOIN users ON likes.user_id WHERE likes.user_id = users.id;";
         using (var multi = cnx.QueryMultiple(query, null))
         {
             var         posts = multi.Read <Post, User, Post>((post, user) => { post.user = user; return(post); }).ToList();
             var         likes = multi.Read <Like, User, Like>((like, user) => { like.user = user; return(like); }).ToList();
             List <Post> Posts = posts.GroupJoin(likes,
                                                 post => post.id,
                                                 like => like.post_id,
                                                 (Post, Like) =>
             {
                 Post.likes = Like.ToList();
                 return(Post);
             }).OrderByDescending(post => post.likes.Count).ToList();
             return(Posts);
         }
     }
 }