Exemplo n.º 1
0
 private IMongoQueryable <ArticleWithRankings> GenerateArticleDetailsQuery(IMongoQueryable <Article> query, string profilerId)
 {
     return(query.GroupJoin(comments.AsQueryable(), x => x.Id, y => y.ArticleId, (x, y) => new { article = x, comments = y })
            .Select(a => new
     {
         Id = a.article.Id,
         Title = a.article.Title,
         Abstract = a.article.Abstract,
         UpdatedOn = a.article.UpdatedOn,
         CreatedOn = a.article.CreatedOn,
         IsApproved = a.article.IsApproved,
         IsPrivate = a.article.IsPrivate,
         Views = a.article.Views,
         Content = a.article.Content,
         AuthorId = a.article.AuthorId,
         Categories = a.article.Categories,
         GalleryImages = a.article.GalleryImages,
         commentsCount = a.comments.Count()
     }).Join(users.AsQueryable(), x => x.AuthorId, y => y.Id, (x, y) => new ArticleDetails
     {
         Id = x.Id,
         Title = x.Title,
         Abstract = x.Abstract,
         UpdatedOn = x.UpdatedOn,
         CreatedOn = x.CreatedOn,
         IsApproved = x.IsApproved,
         IsPrivate = x.IsPrivate,
         Views = x.Views,
         Content = x.Content,
         Categories = x.Categories,
         GalleryImages = x.GalleryImages,
         Author = y,
         CommentsCount = x.commentsCount
     }).GroupJoin(articleRankings.AsQueryable(), x => x.Id, y => y.ArticleId, (x, y) => new ArticleWithRankings
     {
         Id = x.Id,
         Title = x.Title,
         Abstract = x.Abstract,
         UpdatedOn = x.UpdatedOn,
         CreatedOn = x.CreatedOn,
         IsApproved = x.IsApproved,
         IsPrivate = x.IsPrivate,
         Views = x.Views,
         Content = x.Content,
         Categories = x.Categories,
         GalleryImages = x.GalleryImages,
         Author = x.Author,
         CommentsCount = x.CommentsCount,
         Rankings = y,
         Likes = y.Count(a => a.DidILike),
         Dislikes = y.Count(a => a.DidIDislike),
         Favors = y.Count(a => a.DidIFavor)
     }));;
 }
        protected IMongoQueryable <MasterDetails <TMasterField, TDetailField> > QueryIncludeDetail <TDetail, TMasterField,
                                                                                                    TDetailField>(
            IRepositoryBase detailRepository,
            Expression <Func <TDetail, ObjectId> > foreignKeySelector,
            Expression <Func <T, IEnumerable <TDetail>, MasterDetails <TMasterField, TDetailField> > > resultSelector,
            Expression <Func <T, bool> >?masterFilter = null)
            where TDetail : EntityBase
        {
            if (typeof(TMasterField) == typeof(T) ||
                typeof(TDetailField) == typeof(TDetail))
            {
                throw new NotSupportedException("A projection must not include the document itself (MongoDB Driver limitation)");
            }

            // automatically applies transaction
            IMongoQueryable <T> query = Query();

            if (masterFilter != null)
            {
                query = query.Where(masterFilter);
            }

            IMongoQueryable <MasterDetails <TMasterField, TDetailField> > joinedQuery = query
                                                                                        .GroupJoin(GetCollection <TDetail>(detailRepository.CollectionName), m => m.Id,
                                                                                                   foreignKeySelector, resultSelector);

            return(joinedQuery);
        }