gpt4 book ai didi

c# - Automapper 无法翻译通用列表

转载 作者:太空宇宙 更新时间:2023-11-03 21:35:32 25 4
gpt4 key购买 nike

我有两个要映射的以下类的集合:

 public class Client_Crime_ViewModel
{
public Client_Crime_ViewModel() { }

public Client_Crime_ViewModel(CrimeIncident ci)
{
Id = ci.Id;
CaseNumber = ci.CaseNumber;
DateOfIncident = ci.DateOfIncident;
Description = ci.Description;
}

public Int64 Id
{
get;
set;
}

[Required]
[DisplayName("Case Number")]
public string CaseNumber { get; set; }

[DataType(DataType.Date)]
[Required]
[DisplayName("Date of Incident")]
public string DateOfIncident { get; set; }

[DataType(DataType.MultilineText)]
[Required]
public string Description { get; set; }
}



public class CrimeIncident
{
public Int64 Id { get; set; }

[Required]
public string CaseNumber { get; set; }

[Required]
public string Description { get; set; }

[DataType(DataType.Date)]
public string DateOfIncident { get; set; }

public CrimeIncident() { }

public CrimeIncident(string caseNumber, string dateOfIncident, string description)
{
CaseNumber = caseNumber;
Description = description;
DateOfIncident = dateOfIncident;
}
}

我尝试过以下两种方式进行映射:

方法一:

Mapper.CreateMap<List<Client_Crime_ViewModel>, List<CrimeIncident>>();
List<Client_Crime_ViewModel> cvmList = System.Web.Helpers.Json.Decode<System.Collections.Generic.List<Client_Crime_ViewModel>>(rb.Form["CrimeCollection"]);
List<CrimeIncident> ciList = Mapper.Map<List<Client_Crime_ViewModel>, List<CrimeIncident>>(cvmList);

方法二:

Mapper.CreateMap<List<Client_Crime_ViewModel>, List<CrimeIncident>>();
List<Client_Crime_ViewModel> cvmList = System.Web.Helpers.Json.Decode<System.Collections.Generic.List<Client_Crime_ViewModel>>(rb.Form["CrimeCollection"]);
List<CrimeIncident> ciList = Mapper.Map<List<CrimeIncident>>(cvmList);

这两种方法都失败了。调试显示,尽管 cvmList 的元素计数为 > 0,但 ciList 的元素计数仍为 0

知道我做错了什么吗?提前致谢。

最佳答案

您不需要为列表创建映射,只需映射实体即可。

关于c# - Automapper 无法翻译通用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22053888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com