提问人:Martín Cárdenas 提问时间:8/15/2023 最后编辑:Martín Cárdenas 更新时间:8/15/2023 访问量:71
使用 LINQ 进行多次联接
Multiple Joins with LINQ
问:
我有这个疑问:
public IQueryable<PostView> PostJoinQuery()
{
return _context.Posts.GroupJoin(
_context.Users,
post => post.User_Id,
user => user.User_Id,
(posts, users) => new { posts, users })
.SelectMany(
x => x.users.DefaultIfEmpty(),
(post, user) => new PostView
{
User_Id = post.posts.User_Id,
Post_Id = post.posts.Post_Id,
Author = user.Username,
First_Name = user.First_Name,
Last_Name = user.Last_Name,
Created = post.posts.Created,
Modified = post.posts.Modified,
Content = post.posts.Content,
Profile_Picture = user != null ? user.Profile_Picture : "No picture",
})
.GroupJoin(
_context.Comments,
post => post.Post_Id,
comment => comment.Post_Id,
(post, comment) => new
{
PostView = post,
Comments = comment.Count()
})
.Select(result => new PostView
{
User_Id = result.PostView.User_Id,
Post_Id = result.PostView.Post_Id,
Author = result.PostView.Author,
First_Name = result.PostView.First_Name,
Last_Name = result.PostView.Last_Name,
Created = result.PostView.Created,
Modified = result.PostView.Modified,
Content = result.PostView.Content,
Profile_Picture = result != null ? result.PostView.Profile_Picture : "No picture",
Comments = result.Comments
});
}
我需要在其中添加另一个 GroupJoin 与_context。图像将执行与通信相同的操作,但现在我需要它来计算图像
我尝试了以下方法:
public IQueryable<PostView> PostJoinQuery()
{
return _context.Posts.GroupJoin(
_context.Users,
post => post.User_Id,
user => user.User_Id,
(posts, users) => new { posts, users })
.SelectMany(
x => x.users.DefaultIfEmpty(),
(post, user) => new PostView
{
User_Id = post.posts.User_Id,
Post_Id = post.posts.Post_Id,
Author = user.Username,
First_Name = user.First_Name,
Last_Name = user.Last_Name,
Created = post.posts.Created,
Modified = post.posts.Modified,
Content = post.posts.Content,
Profile_Picture = user != null ? user.Profile_Picture : "No picture",
})
.GroupJoin(
_context.Comments,
post => post.Post_Id,
comment => comment.Post_Id,
(post, comment) => new
{
PostView = post,
Comments = comment.Count()
})
.GroupJoin(
_context.Images,
post => post.PostView.Post_Id,
image => image.Post_Id,
(post, image) => new
{
PostView = post,
Images = image.Count()
})
.Select(result => new PostView
{
User_Id = result.PostView.PostView.User_Id,
Post_Id = result.PostView.PostView.Post_Id,
Author = result.PostView.PostView.Author,
First_Name = result.PostView.PostView.First_Name,
Last_Name = result.PostView.PostView.Last_Name,
Created = result.PostView.PostView.Created,
Modified = result.PostView.PostView.Modified,
Content = result.PostView.PostView.Content,
Profile_Picture = result != null ? result.PostView.PostView.Profile_Picture : "No picture",
Comments = result.PostView.Comments,
Images = result.Images
});
}
但是我收到以下错误: 无法翻译。以可翻译的形式重写查询,或通过插入对“AsEnumerable”、“AsAsyncEnumerable”、“ToList”或“ToListAsync”的调用显式切换到客户端评估
编辑:我也试过这个,它计算图像而不是通信网络:
public IQueryable<PostView> PostJoinQuery()
{
return _context.Posts.GroupJoin(
_context.Users,
post => post.User_Id,
user => user.User_Id,
(posts, users) => new { posts, users })
.SelectMany(
x => x.users.DefaultIfEmpty(),
(post, user) => new PostView
{
User_Id = post.posts.User_Id,
Post_Id = post.posts.Post_Id,
Author = user.Username,
First_Name = user.First_Name,
Last_Name = user.Last_Name,
Created = post.posts.Created,
Modified = post.posts.Modified,
Content = post.posts.Content,
Profile_Picture = user != null ? user.Profile_Picture : "No picture",
})
.GroupJoin(
_context.Comments,
post => post.Post_Id,
comment => comment.Post_Id,
(post, comments) => new
{
post,
comments
})
.SelectMany(
x => x.comments.DefaultIfEmpty(),
(post, comments) => new PostView
{
User_Id = post.post.User_Id,
Post_Id = post.post.Post_Id,
Author = post.post.Author,
First_Name = post.post.First_Name,
Last_Name = post.post.Last_Name,
Created = post.post.Created,
Modified = post.post.Modified,
Content = post.post.Content,
Profile_Picture = post.post != null ? post.post.Profile_Picture : "No picture",
//Comments = post.comments.Count() // *** THIS FAILS
})
.GroupJoin(
_context.Images,
post => post.Post_Id,
image => image.Post_Id,
(post, image) => new
{
PostView = post,
Images = image.Count()
})
.Select(result => new PostView
{
User_Id = result.PostView.User_Id,
Post_Id = result.PostView.Post_Id,
Author = result.PostView.Author,
First_Name = result.PostView.First_Name,
Last_Name = result.PostView.Last_Name,
Created = result.PostView.Created,
Modified = result.PostView.Modified,
Content = result.PostView.Content,
Profile_Picture = result != null ? result.PostView.Profile_Picture : "No picture",
Comments = result.PostView.Comments,
Images = result.Images
});
}
我需要正确计算评论数
答: 暂无答案
评论