Search code examples
c#listcollectionscommandviewmodel

Return a list using viewmodel(or commandResult)


I have a HttpGet endpoint, and it should return a collection of transfers, I'm using my Entity Transfer to do that

enter image description here

but I'm trying to change to use my CommandResponse(or viewmodel) GetTransferResponse, and I don't know how to use my var transfer in my CommandResponse

enter image description here

I already use it when it's only one transfer, but with a collection, I Don't know how create the constructor in this case, I think it's going to looks like this, but it's going to be a collection:

enter image description here

my repository:

enter image description here


Solution

  • You can use extension methods to convert ICollection<Transfer> to ICollection<GetTransferResponse>.

    Make sure to include:

    using System.Linq;

    Update your GetAll method to:

    public ICollection<GetTransferResponse> GetAll()
    {
        var transfer = _repo.GetAll()
                            .Select(x => new GetTransferResponse(x))
                            .ToList();
        return transfer;
    }