英文:
Trying to use automapper to map a list of objects
问题
我在MappingProfile.cs中有以下代码:
public class MappingProfile: Profile
{
public MappingProfile()
{
CreateMap<GraphUserDeltaApiDto, User>().ReverseMap();
}
}
在我的代码中,我正在执行以下操作:
graphClient.CheckforUserDeltas();
var graphDtoUsers = graphClient.UserObjects;
List<User> freshUsers = mapper.Map<List<GraphUserDeltaApiDto>>(graphDtoUsers);
我收到的错误消息是:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
HResult=0x80131500 Message=无法隐式转换类型
'System.Collections.Generic.List<Core.Application.Contracts.DTOs.GraphUserDeltaApiDto>'
到 'System.Collections.Generic.List<Core.Domain.Entities.User>'
我找到了这篇文章:
https://stackoverflow.com/questions/5589471/mapping-lists-using-automapper
基于那个文章,我尝试更改我的逻辑如下:
var Users = mapper.Map<List<User>>(List<GraphUserDeltaApiDto>>(graphDtoUsers));
var localSave = UpsertO3MWithLatestUserData(Users);
但现在我得到这个错误:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
HResult=0x80131500
Message=最佳匹配的方法“AutoMapper.IMapperBase.Map<System.Collections.Generic.List<Core.Domain.Entities.User>,System.Collections.Generic.List<Core.Application.Contracts.DTOs.GraphUserDeltaApiDto>>(System.Collections.Generic.List<Core.Domain.Entities.User>)”有一些无效的参数
Source=System.Linq.Expressions
我的理解是,具有相同名称的任何字段将从源对象复制到目标对象。在我的情况下,源数据比目标数据多得多。
我不确定如何调试这个问题。
如有任何提示,将不胜感激。
英文:
I have the following code in my MappingProfile.cs:
public class MappingProfile: Profile
{
public MappingProfile()
{
CreateMap<GraphUserDeltaApiDto, User>().ReverseMap();
}
}
And in my code I'm doing this:
graphClient.CheckforUserDeltas();
var graphDtoUsers = graphClient.UserObjects;
List<User> freshUsers = mapper.Map<List<GraphUserDeltaApiDto>>(graphDtoUsers);
The error I get is:
> Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
> HResult=0x80131500 Message=Cannot implicitly convert type
> 'System.Collections.Generic.List<Core.Application.Contracts.DTOs.GraphUserDeltaApiDto>'
> to 'System.Collections.Generic.List<Core.Domain.Entities.User>'
I found this post:
https://stackoverflow.com/questions/5589471/mapping-lists-using-automapper
Based on that I tried to change my logic to look like this:
var Users = mapper.Map(List<User>,List<GraphUserDeltaApiDto>>(graphDtoUsers);
var localSave = UpsertO3MWithLatestUserData(Users);
but now i get this error:
> Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
> HResult=0x80131500
> Message=The best overloaded method match for 'AutoMapper.IMapperBase.Map<System.Collections.Generic.List<Core.Domain.Entities.User>,System.Collections.Generic.List<Core.Application.Contracts.DTOs.GraphUserDeltaApiDto>>(System.Collections.Generic.List<Core.Domain.Entities.User>)'
> has some invalid arguments
> Source=System.Linq.Expressions
My understanding is that any fields with the same name will be copied from the source to the destination object. In my case, the source has way more data than the destination.
I'm not sure how to debug this.
Any tips would be appreciated.
答案1
得分: 1
以下是翻译好的部分:
You are trying to assign a List<GraphUserDeltaApiDto>
to a List<User>
because mapper.Map<List<GraphUserDeltaApiDto>>(graphDtoUsers);
returns the value of the type specified as a template parameter.
'AutoMapper'中的Map
方法具有以下签名:
/// <summary>
/// 将源对象映射到新的目标对象。
/// 源类型是从源对象推断出来的。
/// </summary>
/// <typeparam name="TDestination">要创建的目标类型</typeparam>
/// <param name="source">要从中映射的源对象</param>
/// <returns>映射后的目标对象</returns>
TDestination Map<TDestination>(object source);
/// <summary>
/// 将源对象映射到新的目标对象。
/// </summary>
/// <typeparam name="TSource">要使用的源类型,不考虑运行时类型</typeparam>
/// <typeparam name="TDestination">要创建的目标类型</typeparam>
/// <param name="source">要从中映射的源对象</param>
/// <returns>映射后的目标对象</returns>
TDestination Map<TSource, TDestination>(TSource source);
因此,在Map<TDestination>
方法中,您必须使用目标类型而不是源类型:
graphClient.CheckforUserDeltas();
var graphDtoUsers = graphClient.UserObjects;
List<User> freshUsers = mapper.Map<List<User>>(graphDtoUsers);
如果使用TDestination Map<TSource, TDestination>(TSource source)
方法,则必须使用源类型作为第一个泛型参数,目标类型作为第二个泛型参数:
var users = mapper.Map<List<GraphUserDeltaApiDto>, List<User>>(graphDtoUsers);
这个规则不仅适用于集合,而且适用于AutoMapper中的所有转换,因为它使用相同的方法。
英文:
You are trying to assign a List<GraphUserDeltaApiDto>
to a List<User>
because mapper.Map<List<GraphUserDeltaApiDto>>(graphDtoUsers);
returns the value of the type specified as a template parameter.
Map
methods in AutoMapper have next signatures:
/// <summary>
/// Execute a mapping from the source object to a new destination object.
/// The source type is inferred from the source object.
/// </summary>
/// <typeparam name="TDestination">Destination type to create</typeparam>
/// <param name="source">Source object to map from</param>
/// <returns>Mapped destination object</returns>
TDestination Map<TDestination>(object source);
/// <summary>
/// Execute a mapping from the source object to a new destination object.
/// </summary>
/// <typeparam name="TSource">Source type to use, regardless of the runtime type</typeparam>
/// <typeparam name="TDestination">Destination type to create</typeparam>
/// <param name="source">Source object to map from</param>
/// <returns>Mapped destination object</returns>
TDestination Map<TSource, TDestination>(TSource source);
So you have to use your destination type instead of source type in Map<TDestination>
method:
graphClient.CheckforUserDeltas();
var graphDtoUsers = graphClient.UserObjects;
List<User> freshUsers = mapper.Map<List<User>>(graphDtoUsers);
And you have to use a source type as first generic parameter and a destination type as second generic parameter if you use TDestination Map<TSource, TDestination>(TSource source)
method:
var users = mapper.Map<List<GraphUserDeltaApiDto>, List<User>>(graphDtoUsers);
This кгду is not specific to collections only, but applies to all conversions in the AutoMapper because it used the same methods.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论