[英].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.