What type of collection should be returned from EFCore 3.1 in WebAPI?

asp.net-core asp.net-core-webapi c# c#-8.0 entity-framework-core

Question

I'm writing ASP.NET Core 3.1 Web API and I caught myself being confused... if I use the right type to return.

So here is my code (I removed paging for simplicity):

public class UserData
{
    public IEnumerable<UserDto> GetUsers()
    {
        var users = _fni.Users.AsNoTracking()
            .OrderBy(row => row.UserName);

        return _mapper.Map<IEnumerable<UserDto>>(users);
    }
}


[HttpGet]
public IActionResult GetUsers()
{
    var users = _userData.GetUsers();

    return Ok(users);
}

Users are queried from the database and immediately returned to the API's client. In C# 8 era, what's the best way to return this collection?

  • IEnumerable
  • List<UserDto>
  • async Task<List<UserDto>>
  • IAsyncEnumerable

EDIT: The question is about what should be returned by EFCore3.1, in the Data Repository in UserData class. Not from the Controller.

1
1
12/14/2019 2:48:15 AM

Popular Answer

Here is the documentation from Microsoft regarding return types from Controller. See if that helps you

https://docs.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-3.1

2
12/13/2019 11:07:10 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow