[英].NET 6 : context.db.Where().Orderby().ToListAsync() does not work
这是我的代码:
public async Task<IEnumerable<Command>> GetCommandsByPlatform(Guid platformId)
{
return await _context.Commands
.Where(c => c.PlatformId == platformId)
.ToListAsync();
}
public async Task<IEnumerable<Command>> GetCommandsByPlatform(Guid platformId)
{
return await _context.Commands
.Where(c => c.PlatformId == platformId)
.OrderBy(c => c.Platform.Name)
.ToListAsync();
}
唯一的区别是第二个代码在Where()
OrderBy()
) 。 第一个代码片段成功返回命令列表,但第二个代码片段仅返回一个空列表。
谢谢你的回答。
乍一看,您的查询中需要一个Include(x => x.Platform)
:
public async Task<IEnumerable<Command>> GetCommandsByPlatform(Guid platformId)
{
return await _context.Commands
.Include(c => c.Platform) // this generates a join
.Where(c => c.PlatformId == platformId)
.OrderBy(c => c.Platform.Name)
.ToListAsync();
}
我有 75% 的把握在 EF Core 中不需要Include
,而在完整的 EF6 中,没有它就不会生成连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.