尝试使用AutoMapper将对象列表进行映射。

huangapple go评论86阅读模式
英文:

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&lt;GraphUserDeltaApiDto, User&gt;().ReverseMap();
    }

}

And in my code I'm doing this:

 graphClient.CheckforUserDeltas();
 var graphDtoUsers = graphClient.UserObjects;
 List&lt;User&gt; freshUsers = mapper.Map&lt;List&lt;GraphUserDeltaApiDto&gt;&gt;(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&lt;User&gt;,List&lt;GraphUserDeltaApiDto&gt;&gt;(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&lt;GraphUserDeltaApiDto&gt; to a List&lt;User&gt; because mapper.Map&lt;List&lt;GraphUserDeltaApiDto&gt;&gt;(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&lt;TDestination&gt;方法中,您必须使用目标类型而不是源类型:

graphClient.CheckforUserDeltas();
var graphDtoUsers = graphClient.UserObjects;
List&lt;User&gt; freshUsers = mapper.Map&lt;List&lt;User&gt;&gt;(graphDtoUsers);

如果使用TDestination Map&lt;TSource, TDestination&gt;(TSource source)方法,则必须使用源类型作为第一个泛型参数,目标类型作为第二个泛型参数:

var users = mapper.Map&lt;List&lt;GraphUserDeltaApiDto&gt;, List&lt;User&gt;&gt;(graphDtoUsers);

这个规则不仅适用于集合,而且适用于AutoMapper中的所有转换,因为它使用相同的方法。

英文:

You are trying to assign a List&lt;GraphUserDeltaApiDto&gt; to a List&lt;User&gt; because mapper.Map&lt;List&lt;GraphUserDeltaApiDto&gt;&gt;(graphDtoUsers); returns the value of the type specified as a template parameter.

Map methods in AutoMapper have next signatures:

/// &lt;summary&gt;
/// Execute a mapping from the source object to a new destination object.
/// The source type is inferred from the source object.
/// &lt;/summary&gt;
/// &lt;typeparam name=&quot;TDestination&quot;&gt;Destination type to create&lt;/typeparam&gt;
/// &lt;param name=&quot;source&quot;&gt;Source object to map from&lt;/param&gt;
/// &lt;returns&gt;Mapped destination object&lt;/returns&gt;
TDestination Map&lt;TDestination&gt;(object source);

/// &lt;summary&gt;
/// Execute a mapping from the source object to a new destination object.
/// &lt;/summary&gt;
/// &lt;typeparam name=&quot;TSource&quot;&gt;Source type to use, regardless of the runtime type&lt;/typeparam&gt;
/// &lt;typeparam name=&quot;TDestination&quot;&gt;Destination type to create&lt;/typeparam&gt;
/// &lt;param name=&quot;source&quot;&gt;Source object to map from&lt;/param&gt;
/// &lt;returns&gt;Mapped destination object&lt;/returns&gt;
TDestination Map&lt;TSource, TDestination&gt;(TSource source);

So you have to use your destination type instead of source type in Map&lt;TDestination&gt; method:

graphClient.CheckforUserDeltas();
var graphDtoUsers = graphClient.UserObjects;
List&lt;User&gt; freshUsers = mapper.Map&lt;List&lt;User&gt;&gt;(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&lt;TSource, TDestination&gt;(TSource source) method:

 var users = mapper.Map&lt;List&lt;GraphUserDeltaApiDto&gt;, List&lt;User&gt;&gt;(graphDtoUsers);

This кгду is not specific to collections only, but applies to all conversions in the AutoMapper because it used the same methods.

huangapple
  • 本文由 发表于 2023年2月24日 05:48:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定