gpt4 book ai didi

c# - 填充对象列表(使用 Linq?)

转载 作者:太空狗 更新时间:2023-10-30 00:09:18 24 4
gpt4 key购买 nike

我想知道是否有更好的方法来执行以下操作,

    IList<RoleViewModel> ReturnViewModel = new List<RoleViewModel>();

IList<Role> AllRoles = PermServ.GetAllRoles();

foreach (var CurRole in AllRoles)
{
ReturnViewModel.Add(new RoleViewModel(CurRole));
}

它的代码非常简单,只需获取数据对象并将其转换为 ViewModel。我想知道是否有办法更好地做到这一点? - 也许使用 Linq?

最佳答案

从我的头顶(不是通过开发机器)。

IList<RoleViewModel> returnViewModel = PermServ.GetAllRoles()
.Select(x => new RoleViewModel(x))
.ToList();

关于c# - 填充对象列表(使用 Linq?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1840537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com