Example #1
0
        public IEnumerable <PostView> GetUserBestPost(int userId)
        {
            var posts = _db.Posts
                        .Include(p => p.User)
                        .Include(p => p.Category)
                        .Include(p => p.AssociatedTags)
                        .ThenInclude(pt => pt.Tag)
                        .Where(x => x.UserId == userId)
                        .OrderByDescending(x => _db.Ratings
                                           .Where(r => r.PostId == x.id)
                                           .Average(r => (int?)r.rating) ?? 0)
                        .Take(5)
                        .ToList();

            var items = new List <PostView>();

            foreach (var item in posts)
            {
                items.Add(postMapper.From(item));
            }

            return(items);
        }