在 EF Core Linq 查询中连接表
Joining tables in EF Core Linq Query
我目前正在尝试使用 EF Core 制作 Web Api,但我 运行 在加入我整理好的表格时遇到了一些问题。我正在使用以下数据库图:
我目前从 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 一起工作,我会将用户加入 'comments',但我不能,所以我尝试了 similar-ish 解决方案,我认为它会起作用.
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;我可以将用户加入 post,并评论 post,但我无法将用户绑定到评论
我希望你能帮助我,提前致谢:)
编辑:这是我的模型
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; }
}
要获得带有评论(以及每个评论的用户)和 post 用户的 post,只需使用 .Include()
var res = this._dbContext.Posts
.Include(p => p.User)
.Include(p => p.Comments)
.ThenInclude(c => c.User)
.ToList();
如果您不喜欢引入第 3 方库,例如 AutoMapper
,您可以创建一个 convertPostToDto
函数并使用 .
res.Select(p => convertPostToDto(p))
将res
转换为结果
我目前正在尝试使用 EF Core 制作 Web Api,但我 运行 在加入我整理好的表格时遇到了一些问题。我正在使用以下数据库图:
我目前从 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 一起工作,我会将用户加入 'comments',但我不能,所以我尝试了 similar-ish 解决方案,我认为它会起作用.
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;我可以将用户加入 post,并评论 post,但我无法将用户绑定到评论
我希望你能帮助我,提前致谢:)
编辑:这是我的模型
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; }
}
要获得带有评论(以及每个评论的用户)和 post 用户的 post,只需使用 .Include()
var res = this._dbContext.Posts
.Include(p => p.User)
.Include(p => p.Comments)
.ThenInclude(c => c.User)
.ToList();
如果您不喜欢引入第 3 方库,例如 AutoMapper
,您可以创建一个 convertPostToDto
函数并使用 .
res.Select(p => convertPostToDto(p))
将res
转换为结果