我尝试这个查询已经有一段时间了,但是我似乎不能实现,问题是我不能只返回我想要的那些列
var thread = context.threads.Include(t => t.posts).ThenInclude(p => p.ApplicationUser).Include(t => t.ApplicationUser).Include(t => t.movie).Where(t => t.Id == id)
该查询返回ApplicationUser的所有信息,包括电子邮件和散列密码,当然我不希望我尝试这样做
var thread = context.threads.Include(t => t.posts).ThenInclude(p => p.ApplicationUser).Include(t => t.ApplicationUser).Include(t => t.movie).Where(t => t.Id == id).Select(t => new
{
title = t.title,
body = t.body,
threadUserName = t.ApplicationUser.UserName,
postsThread = t.posts
});
但是我遇到了一个障碍,当我必须查询帖子的UserName,所以数据从ThenInclude,尝试做一些像t.posts.ApplicationUser.UserName
和类似的事情,但它不工作,我如何查询帖子的用户名?核心是从Identity net- ApplicationUser包中的IdentityUser类派生的类。
发布于 2019-05-01 06:12:01
为了查询postsThread
,您可以添加一个新的Select
,如下所示:
var thread = _appDbContext.Threads
.Include(t => t.Posts)
.ThenInclude(p => p.ApplicationUser)
.Include(t => t.ApplicationUser)
.Where(t => t.Id == id)
.Select(t => new
{
title = t.Title,
body = t.Body,
threadUserName = t.ApplicationUser.UserName,
postsThread = t.Posts.Select(p => new {
p.Content,
p.ApplicationUser.UserName
})
})
.ToList();
https://stackoverflow.com/questions/55924394
复制