在 LinqToDb 中使用 LoadWith 和 ThenLoad 的四个嵌套关联

Four nested associations using LoadWith and ThenLoad in LinqToDb

我有一个关于使用 LoadWith 和 ThenLoad 的问题。我要搞定第四个协会

我可以获得第三个协会。比如Post -> CreatedUser -> UserDetail,但是我不能关联第四个。

我无法获取以下查询的 UserDetail。 UserDetail returns 空。

Post -> Post评论 -> CreatedUser -> UserDetail

如何解决我的查询?

        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();

认为 LoadWith 可以使用自定义投影是错误的。为了正确翻译,简化了以下查询:

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();