crapcoder
crapcoder

Reputation: 25

C# EF Core, ASP.NET Core Web API list items

I am at a loss for where to look to understand how to use EF Core and ASP.NET Core Web API.

I am building a small marina manager and I am able to get my independent list of marinas and my independent list of docks but I want to get a list of all docks that belong to a Marina

These are my database tables:

Marina

Int Id
String Name

Dock

Int Id
Int MarinaId
String Name

I am attempting to output all docks for a given MarinaID where the MarinaId is equal to the input passed. But I am running into issues everywhere I turn.

I am not asking for the solution here just advice or direction on where I can figure this out. I have tried using _context.Dock.Find which wasn't successful. Please point me in the direction of the right documentation to help me solve this issue.

I am currently using ControllerBase.

Below is how I get the results for all of the docks or marinas respectively I just change the name of the field from Marina to Docks for docks call but its essentially the same. I have tried passing an int and even the model but ultimately I need to find the syntax to properly do what I am attempting.

[Route("List")]
[AllowAnonymous]
[HttpGet]
public async Task<ActionResult<List<Marina>>> GetResultAsync()
{
    var itemLst = _context.Marina.ToList();
    var results = new List<Marina>(itemLst);
    await Task.CompletedTask;
    return results;
}

I have tried this also to no avail it just lists all of the docks still

public async Task<ActionResult<List<Dock>>> ListDocks(int marinaId)
{
    var itemList = _context.Dock.Select(dock => new Dock
    {
        Id = dock.Id,
        Name = dock.Name,
        MarinaId = marinaId
    }).ToList();

    var results = new List<Dock>(itemLst);
    await Task.CompletedTask;

    return results;
}

Upvotes: 0

Views: 211

Answers (1)

Emmanuel Herrera
Emmanuel Herrera

Reputation: 141

public async Task<ActionResult<List<Dock>>> ListDocks(int marinaId)
{
    return _context.Dock.Where(d => d.MarinaId == marinaId).ToList();
}

Upvotes: 1

Related Questions