Automapper无法映射IEnumerable <t> </t>

时间:2014-03-30 20:13:36

标签: asp.net-mvc ienumerable automapper

这是我得到的例外:

Missing type map configuration or unsupported mapping.

Mapping types:
CompanyViewModel -> Company
Razor.Sample.ViewModels.CompanyViewModel.CompanyViewModel -> Model.Company

Destination path:
CompanyListViewModel.Companies.Companies.Companies0[0]

Source value:
Razor.Sample.ViewModels.CompanyViewModel.CompanyViewModel

当我运行此代码时:

var companyViewModels = Mapper.Map<IEnumerable<Company>, IEnumerable<CompanyViewModel>>(companies);
var companyListViewModel = Mapper.Map<IEnumerable<CompanyViewModel>, CompanyListViewModel>(companyViewModels);

这就是我的映射器配置文件:

CreateMap<IEnumerable<CompanyViewModel>, CompanyListViewModel>()
    .ForMember(dest => dest.TopRankings, o => o.MapFrom(src => src.Select(c => c.TopRanking)))
    .ForMember(dest => dest.TotalCompaniesWithOpenPositions,
        o => o.MapFrom(src => src.Count(c => c.HasOpenPositions == true)))
    .ForMember(dest => dest.TotalEmployees,
        o => o.MapFrom(src => src.Select(c => c.NumberOfEmployees).Sum<int>(s => s)))
    .ForMember(dest => dest.Companies, o => o.MapFrom(src => src.Select(c => c)));

我知道CreateMap不应该有IEnumerable<T> 但是我可以从每个CompanyViewModel及其属性映射以创建一个带LINQ的Sum 并将其分配给{ {1}}?我需要配置文件中的可枚举,而不是在我调用dest.TotalEmployees的mvc控制器中。

0 个答案:

没有答案