From 078a4b540b71f031d4b639b163d3c6d7970eaf2e Mon Sep 17 00:00:00 2001 From: enisn Date: Mon, 12 Apr 2021 17:47:04 +0300 Subject: [PATCH] CmsKit - Fix MongoDb Query for Comment details --- .../Comments/MongoCommentRepository.cs | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs index 1fc4feab43..3f7b95ed40 100644 --- a/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs +++ b/modules/cms-kit/src/Volo.CmsKit.MongoDB/Volo/CmsKit/MongoDB/Comments/MongoCommentRepository.cs @@ -23,23 +23,28 @@ namespace Volo.CmsKit.MongoDB.Comments public async Task GetWithAuthorAsync(Guid id, CancellationToken cancellationToken = default) { - var query = from comment in (await GetMongoQueryableAsync(cancellationToken)) - join user in (await GetDbContextAsync(cancellationToken)).CmsUsers on comment.CreatorId equals user.Id - where id == comment.Id - select new CommentWithAuthorQueryResultItem - { - Comment = comment, - Author = user - }; + var dbContext = await GetDbContextAsync(); + var commentQueryable = await GetMongoQueryableAsync(cancellationToken); + var userQueryable = dbContext.Collection(); + + var joined = commentQueryable.Join(userQueryable, x => x.CreatorId, x => x.Id, (comment, user) => new + { + Comment = comment, + User = user + }); - var commentWithAuthor = await query.FirstOrDefaultAsync(GetCancellationToken(cancellationToken)); + var commentWithAuthor = await joined.FirstOrDefaultAsync(GetCancellationToken(cancellationToken)); if (commentWithAuthor == null) { throw new EntityNotFoundException(typeof(Comment), id); } - return commentWithAuthor; + return new CommentWithAuthorQueryResultItem() + { + Comment = commentWithAuthor.Comment, + Author = commentWithAuthor.User + }; } public async Task> GetListAsync(