Home > database >  Blazor API controller GET with dictionary
Blazor API controller GET with dictionary

Time:12-29

I'm looking to implement a GET method within my api controller which is able to return a dictionary. The default api controller returns an IEnumerable however I would like it to work with a dictionary. The dictionary I will be using looks like Dictionary<Guid, Company> with company being my object.

My Api controller

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

CodePudding user response:

I think you mean:

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

Not really sure what relevance the "My Company Dictionary" code block has.. Also not sure why you'd want to return a dictionary, given that it's repeating in the Key info that is in the Value, but I tend to use constructs like this for lookups/combos etc (except there it's a guid/string)

  • Related