【问题标题】:Return a list using viewmodel(or commandResult)使用 viewmodel(或 commandResult)返回一个列表
【发布时间】:2020-12-11 09:01:57
【问题描述】:

我有一个 HttpGet 端点,它应该返回一个传输集合,我正在使用我的实体 Transfer 来做到这一点

但我正在尝试更改为使用我的 CommandResponse(或视图模型)GetTransferResponse,但我不知道如何在我的 CommandResponse 中使用我的 var transfer

我已经使用了它只有一次传输,但是使用集合,我不知道在这种情况下如何创建构造函数,我认为它看起来像这样,但它会是一个集合:

我的仓库:

【问题讨论】:

    标签: c# list collections command viewmodel


    【解决方案1】:

    您可以使用扩展方法将ICollection<Transfer> 转换为ICollection<GetTransferResponse>

    确保包括:

    using System.Linq;

    将您的 GetAll 方法更新为:

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

    【讨论】:

    • 如果这回答了您的问题。能否请您投票并将其标记为答案。这可能对其他人有所帮助。
    • 我无法投票,因为我的声誉低于 15 岁,但我标记为答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    相关资源
    最近更新 更多