1

I have a question that is about using LoadWith and ThenLoad. I need to get the fourth association.

I can get the third association. For instance, Post -> CreatedUser -> UserDetail, but I can not the fourth association.

I can not get UserDetail for the following query. UserDetail returns null.

Post -> PostComment -> CreatedUser -> UserDetail

How can I fix my query?

        IEnumerable<PostListDto> data = _postRepository.Table
        .LoadWith(x => x.CreatedUser).ThenLoad(x => x.UserDetail)
        .LoadWith(x => x.PostImages).LoadWith(x => x.PostVideos)
        .LoadWith(x => x.PostComments).ThenLoad(y => y.CreatedUser).ThenLoad(mt => mt.UserDetail) //here is the fourth
        .Select(p => new PostListDto
        {
            Id = p.Id,
            Text = p.Text,
            CreatedDate = p.CreatedDate,
            ImageUrlList = p.PostImages.Count > 0 ? p.PostImages.Select(x => x.ImageUrl).ToList() : new List<string>(),
            VideoUrl = p.PostVideos.Count == 0 ? "" : p.PostVideos.FirstOrDefault().VideoUrl,
            CreatedByUserName = p.CreatedUser == null ? "" : p.CreatedUser.UserName,
            CreatedByUserPhoto = p.CreatedUser == null ? "" : p.CreatedUser.UserDetail.ProfilePhotoPath,
            PostType = p.PostType,
            FancyboxData = $"post{p.Id}",
            Comments = p.PostComments.ToList().Select(y => new PostCommentListDto
            {
                Text = y.Text,
                CreatedDate = y.CreatedDate,
                Id = y.Id,
                CreatedByUserName = y.CreatedUser == null ? "" : y.CreatedUser.UserName,
                CreatedByUserPhoto = y.CreatedUser == null ? "" : 
            y.CreatedUser.UserDetail.ProfilePhotoPath, **//UserDetail returns null**
                PostId = y.PostId
            }).ToList()                  
        }).OrderByDescending(sa => sa.CreatedDate).AsEnumerable();
Ismail Dogan
  • 295
  • 2
  • 20

1 Answers1

3

It is mistake thinking that LoadWith will work with custom projection. The following query is simplified for correct translation:

var data = _postRepository.Table
    .Select(p => new PostListDto
    {
        Id = p.Id,
        Text = p.Text,
        CreatedDate = p.CreatedDate,
        ImageUrlList = p.PostImages.Select(x => x.ImageUrl).ToList(),
        VideoUrl = p.PostVideos.FirstOrDefault().VideoUrl ?? "",
        CreatedByUserName = p.CreatedUser.UserName ?? "",
        CreatedByUserPhoto = p.CreatedUser.UserDetail.ProfilePhotoPath ?? "",
        PostType = p.PostType,
        FancyboxData = $"post{p.Id}",
        Comments = p.PostComments.Select(y => new PostCommentListDto
        {
            Text = y.Text,
            CreatedDate = y.CreatedDate,
            Id = y.Id,
            CreatedByUserName = y.CreatedUser.UserName ?? "",
            CreatedByUserPhoto = y.CreatedUser.UserDetail.ProfilePhotoPath ?? "", 
            PostId = y.PostId
        }).ToList()                  
    })
    .OrderByDescending(sa => sa.CreatedDate)
    .AsEnumerable();
Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32