使用Automapper

时间:2018-02-06 21:00:59

标签: c# linq automapper

我想知道如何使用Automapper将包含逗号分隔的字符串列表的字符串属性转换为INT列表。我在堆栈溢出中发现了类似的问题,但是当我执行LINQ时,它会在我的结尾处抛出一个错误。我目前正在使用带有Entity Framework 6的C#7和Automapper v 5.1.1(带有Automapper.EF6 v0.5.0)。

数据源示例:Here is my dataSource for that property

ERROR: LINQ to Entities does not recognize the method 'System.Collections.Generic.List`1[System.Int32] GetCommaDelimitedStringAsAListOfInt(System.String)' method, and this method cannot be translated into a store expression.

以下是我的类,我的自动播放器配置和方法。

public class PracticeSource
{
  public string PracticeIds { get; set; } // will look something like "10, 589, 2587,"
}

public class PracticeDestination
{
  public List<int> PracticeIds { get; set; }
}

public static class AutoMapperConfiguration
{
  public static IMapper New Initialize()
  {
    var configuration = new MapperConfiguration(cfg =>
    {
      cfg.CreateMap<PracticeContractOverview, PracticeContractOverviewReadModel>().ForMember(dest => dest.PracticeIds, opt => opt.MapFrom(src => GetCommaDelimitedStringAsAListOfInt(src.PracticeIds)));              
    })
    return configuration.CreateMapper();
  }

  private static List<int> GetCommaDelimitedStringAsAListOfInt(string model)
  {
    if (string.IsNullOrEmpty(model))
    {
      return null;
    }
    return model.Split(',').Select(int.Parse).ToList();
  }
}

0 个答案:

没有答案