如何映射与ValueInjector名单 [英] How to map lists with ValueInjector

查看:135
本文介绍了如何映射与ValueInjector名单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ASP.NET MVC 3

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

 变种人=新PersonRepository()获取()。VAR personViewModel =新PersonViewModel();
personViewModel.InjectFrom< LoopValueInjection>(人)
     .InjectFrom< CountryToLookup>(人);

我有我的首页视图的网格。每一行是一个 CategoryViewModel 的实例。所以我要做的就是让所有的分类列表,然后映射每个类别来一个CategoryViewModel,再通过CategoryViewModels到视图列表中。侯我会做一个这样的映射呢?

 的IEnumerable<类别及GT;所属分类= categoryService.GetAll();

我想下面的工作,但它不会:

  //映射
IList的< CategoryViewModel> viewModelList =新的List< CategoryViewModel>();
viewModelList.InjectFrom(所属分类);


解决方案

据我所知价值喷油器不支持自动集合映射像AutoMapper但你可以使用一个简单的LINQ前pression和操作每个元素:

 的IEnumerable<类别及GT;所属分类= categoryService.GetAll();
IList的< CategoryViewModel> viewModelList =所属分类
    。选择(X =方式>新CategoryViewModel()InjectFrom(X))演员LT; CategoryViewModel>()
    .ToList();

I am using ASP.NET MVC 3.

Can someone please help me clarify what's happening here:

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

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

I have a grid on my Index view. Each row is an instance of a CategoryViewModel. So what I do is to get a list of all the categories and then map each Category to a CategoryViewModel, and then pass this list of CategoryViewModels to the view. Hou would I do a mapping like that?

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

I thought the following would work but it doesn't:

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

解决方案

AFAIK value injecter doesn't support automatic collection mapping like AutoMapper but you could use a simple LINQ expression and operate on each element:

IEnumerable<Category> categoryList = categoryService.GetAll();
IList<CategoryViewModel> viewModelList = categoryList
    .Select(x => new CategoryViewModel().InjectFrom(x)).Cast<CategoryViewModel>()
    .ToList();

这篇关于如何映射与ValueInjector名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆