加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何使用ValueInjector映射列表

发布时间:2020-12-16 00:48:36 所属栏目:asp.Net 来源:网络整理
导读:我正在使用ASP.NET MVC 3。 有人可以帮我澄清这里发生了什么: var person = new PersonRepository().Get();var personViewModel = new PersonViewModel();personViewModel.InjectFromLoopValueInjection(person) .InjectFromCountryToLookup(person); 我的
我正在使用ASP.NET MVC 3。

有人可以帮我澄清这里发生了什么:

var person = new PersonRepository().Get();

var personViewModel = new PersonViewModel();
personViewModel.InjectFrom<LoopValueInjection>(person)
     .InjectFrom<CountryToLookup>(person);

我的索引视图上有一个网格。每一行都是一个CategoryViewModel的一个实例。所以我做的是获取所有类别的列表,然后将每个类别映射到CategoryViewModel,然后将此CategoryViewModels列表传递给视图。那我会做一个这样的映射

IEnumerable<Category> categoryList = categoryService.GetAll();

我以为以下的工作,但它没有:

// Mapping
IList<CategoryViewModel> viewModelList = new List<CategoryViewModel>();
viewModelList.InjectFrom(categoryList);

解决方法

AFAIK值注入器不支持自动收集映射,如AutoMapper,但您可以使用简单的LINQ表达式并对每个元素进行操作:
IEnumerable<Category> categoryList = categoryService.GetAll();
IList<CategoryViewModel> viewModelList = categoryList
    .Select(x => new CategoryViewModel().InjectFrom(x)).Cast<CategoryViewModel>()
    .ToList();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读