EF Core Linq查询中的联接表

时间:2018-10-23 14:45:15

标签: linq entity-framework-core asp.net-core-webapi linq-query-syntax

我目前正在尝试使用EF Core制作Web Api,但在连接我聚集在一起的表时遇到了一些问题。我正在使用以下数据库图: enter image description here

我当前从我的API中获得的数据看起来像这样:

[  
   {  
      "postId":1,
      "postDate":"2018-10-21T21:56:43.9838536",
      "content":"First entry in posts!",
      "user":{  
         "userId":1,
         "creationDate":"2018-10-21T21:56:36.3539549",
         "name":"Hansel"
      },
      "comments":[  
         {  
            "commentId":1,
            "postDate":"0001-01-01T00:00:00",
            "content":"Nice!",
            "user":null
         },
         {  
            "commentId":2,
            "postDate":"0001-01-01T00:00:00",
            "content":"Cool, here's another comment",
            "user":null
         },
         {  
            "commentId":3,
            "postDate":"0001-01-01T00:00:00",
            "content":"and the last one for the night",
            "user":null
         }
      ]
   },
   {  
      "postId":2,
      "postDate":"2018-10-22T21:56:44.0650102",
      "content":"Its good to see that its working!",
      "user":{  
         "userId":2,
         "creationDate":"2018-10-16T21:56:36.4585213",
         "name":"Chris"
      },
      "comments":[  

      ]
   }
]

正如您所看到的,它几乎可以正常工作,我的问题是注释中的空值,我也希望能够吸引用户。但是出于某种原因,我不能。

我当前的查询如下(我正在使用DTO清理生成的JSON):

var result = from post in context.Posts
                join user in context.Users 
                on post.User.UserId equals user.UserId
                join comment in context.Comments                            
                on post.PostId equals comment.Post.PostId into comments
                select new PostDTO
                {
                    Content = post.Content,
                    PostDate = post.PostDate,
                    User = UserDTO.UserToDTO(user),
                    Comments = CommentDTO.CommentToDTO(comments.ToList()),
                    PostId = post.PostId
                };

如果我使用SQL,我会将用户加入到“注释”中,但是我不能,所以我尝试了类似的解决方案,我认为这是可行的。

var result = from post in context.Posts
                join user in context.Users on post.User.UserId equals user.UserId
                join comment in 
                    (from u in context.Users
                    join c in context.Comments
                    on u.UserId equals c.User.UserId select c)
                on post.PostId equals comment.Post.PostId into comments
                select new PostDTO
                {
                    Content = post.Content,
                    PostDate = post.PostDate,
                    User = UserDTO.UserToDTO(user),
                    Comments = CommentDTO.CommentToDTO(comments.ToList()),
                    PostId = post.PostId
                };

但是,尽管此查询确实执行了结果,但与我编写的第一个查询相同,但问题是用户未加入评论

TLDR;我可以加入用户发表评论,也可以发表评论,但是我无法将用户发表评论

我希望您能为我提供帮助,在此先感谢:)

编辑:这是我的模型

public class Comment
{
    public int CommentId { get; set; }
    public DateTime PostDate { get; set; }
    public string Content { get; set; }

    public User User { get; set; }

    public Post Post { get; set; }
}

  public class User
    {
        public int UserId { get; set; }
        public DateTime CreationDate { get; set; }
        public string Name{ get; set; }

        public ICollection<Post> Posts { get; set; }
        public ICollection<Comment> Comments { get; set; }
    }

public class Post
{
    public int PostId { get; set; }
    public DateTime PostDate { get; set; }
    public string Content { get; set; }
    public User User { get; set; }

    public ICollection<Comment> Comments { get; set; }
}

1 个答案:

答案 0 :(得分:2)

要获取带有评论的帖子(以及每个评论的用户)和帖子用户,只需使用.Include()

var res = this._dbContext.Posts
        .Include(p => p.User)
        .Include(p => p.Comments)
            .ThenInclude(c => c.User)
        .ToList();

enter image description here

如果您不想引入诸如AutoMapper之类的第三方库,则可以创建一个convertPostToDto函数并使用。

res.Select(p => convertPostToDto(p))

res转换为结果