【发布时间】:2021-02-06 09:16:19
【问题描述】:
我创建了一个映射器类,将我的事件模型映射到我的 EventEntity 模型,这些模型有一个列表属性(代码片段如下所示);
我遇到了这个C# - Cannot implicitly convert type List<Product> to List<IProduct>
但我在尝试将解决方案实施到我的项目中时遇到了困难。
//Event Model
public List<EventData> EventData { get; set; }
//Event Entity Model
public List<EventDataEntity> EventData { get; set; }
我的 Mapper 类(代码片段)
public class EventToEventEntityMapper : IMapper<Event, EventEntity>
{
public EventEntity Map(Event e)
{
var entity = new EventEntity
{
EventData = e.EventData.Select(Map).ToList() //Modified the code a per user comment
};
return entity;
}
}
//The Interface
public interface IMapper<TSource, TDestination>
{
TDestination Map(TSource entity);
}
我遇到的错误
Severity Code Description Project File Line Suppression State
Error CS0411 The type arguments for method 'Enumerable.Select<TSource, TResult>(IEnumerable<TSource>, Func<TSource, int, TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly
【问题讨论】:
-
拥有一个具有将一种类类型转换为另一种类类型的函数的类不会神奇地列出要自动转换的类型。手动转换它们或根据这两种列表类型创建两个自定义类型并使用operator overload
标签: c#