Blazor API 控制器 GET 与字典

Blazor API controller GET with dictionary

我希望在我的 api 控制器中实现 GET 方法,该方法能够 return 字典。默认的 api 控制器 return 是一个 IEnumerable 但是我希望它能与字典一起使用。我将使用的字典看起来像 Dictionary ,公司是我的对象。

我的Api控制器

[HttpGet]
public async Task<ActionResult<IEnumerable<Company>>> GetCompanies()
{
    return await _context.Companies.ToListAsync();
}

我想你的意思是:

[HttpGet]
public async Task<ActionResult<IDictionary<Guid, Company>>> GetCompanies()
{
    return await _context.Companies.ToDictionaryAsync(c => c.Id);
}

不太确定“我的公司词典”代码块有什么相关性。也不确定为什么要 return 字典,因为它在关键信息中重复值,但我倾向于将这样的结构用于 lookups/combos 等(除了 guid/string)