使用映射一个AutoMapper分组集合

编程入门 行业动态 更新时间:2024-10-27 06:28:23
本文介绍了使用映射一个AutoMapper分组集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有下面的代码,这是行不通的:

I have the following code which is not working:

var groupedZones = this._zoneDataManager.GetZonesGroupedByCountry(); IEnumerable<IGrouping<String, ZoneDTO>> zonesToReturn = Mapper.Map<IEnumerable<IGrouping<String, Zone>>, IEnumerable<IGrouping<String, ZoneDTO>>>(groupedZones);

我不断收到以下异常:

I keep getting the following exception:

价值 \System.Collections.Generic.List 1 [SCGRE.Business.Model.ZoneDTO] \是不是型 \System.Linq.IGrouping 2 [System.String,SCGRE.Business.Model.ZoneDTO] \和不能这个通用集合中的使用。 \r\\\Parameter名:值

The value \"System.Collections.Generic.List1[SCGRE.Business.Model.ZoneDTO]\" is not of type \"System.Linq.IGrouping2[System.String,SCGRE.Business.Model.ZoneDTO]\" and cannot be used in this generic collection.\r\nParameter name: value

我不明白为什么它正试图映射列表< T> 到 IGrouping<弦乐,T> 或也许我没有正确理解例外...但是我基本上有一个的IEnumerable< IGrouping<弦乐,区>> ,我想它映射到的IEnumerable< IGrouping<弦乐,ZoneDTO>>

I do not understand why it is trying to map a List<T> into an IGrouping<String, T> or maybe I did not understand the exception properly... But I basically have an IEnumerable<IGrouping<String, Zone>> and I want to map it to IEnumerable<IGrouping<String, ZoneDTO>>

这是我创建了一个地图从区到 ZoneDTO注如下:

Note that I have created a map from Zone to ZoneDTO as follows:

Mapper.CreateMap<Zone, ZoneDTO>();

这是因为这两个类几乎完全一样的属性。

And that's because both classes have almost exactly the same properties.

任何想法?

推荐答案

在一些工作以防万一,我想你不能做某些事情像

After some work arround, I guess you cannot do some thing like

Mapper.CreateMap<IEnumerable<IGrouping<String, Zone>>, IEnumerable<IGrouping<String, ZoneDTO>>>()

要具体,AutoMapper支持源集合类型包括:(仅通用类型)

To be specific, AutoMapper supports the source collection types include: (Only Generic types)

IEnumerable, IEnumerable<T>, ICollection, ICollection<T>, IList, IList<T>, List<T>, Arrays

AutoMapper将不支持 IGrouping ,因为它不通用枚举类型。

AutoMapper will not support IGrouping because it is non-generic enumerable type.

相反,你可以做以下简单的办法,

Instead you can do the following simple approach,

var zones = this._zoneDataManager.GetZones(); // Write new method in your ZoneDataManager var zoneDTOs = Mapper.Map<List<Zone>, List<ZoneDTO>>(zones); IEnumerable<IGrouping<String, ZoneDTO>> zonesToReturn = zoneDTOs.GroupBy(z => z.Country);

请阅读的这个。希望这有助于。

Please read this. Hope this helps.

更多推荐

使用映射一个AutoMapper分组集合

本文发布于:2023-11-17 06:32:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609164.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AutoMapper

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!