Using string.Split() in AutoMapper issue

Related searches

I have an ASP .Net core application. I am simply trying to have my AutoMapper configure to convert a string comma delimited into a list of strings as per this configuration:

configuration.CreateMap<Job, JobDto>()
             .ForMember(dto => dto.Keywords, options => options.MapFrom(entity => entity.Keywords.Split(',').ToList()))

For some reason it does not get compiled and give me the following error:

An expression tree may not contain a call or invocation that uses optional argument

I can't see why I am getting this error. I am pretty sure that I have done that in my other projects before without any such error.

This is completely true.

Error is raised because expression tree being created is about to contain some more complex logic, like .Split(',').ToList(), which is not an accessible property or method, only top-level reflected object properties and methods are supported (like in class MemberInfo).

Property chaining, deep-calls (.obj1property.obj2property), extension methods are not supported by the expression trees, like in this .ToList() call.

My solution was like this:

// Execute a custom function to the source and/or destination types after member mapping
configuration.CreateMap<Job, JobDto>()
  .AfterMap((dto,jobDto)=>jobDto.Keywords = dto.Keywords.Split(',').ToList());

[SOLVED], Automapper mapping issue concerning comma-delimited varchar field String. IsNullOrWhiteSpace(src.numberSet) ? src.numberSet.Split(',') .Select(s => s.Trim ()) .ToEnumerable() : null)); // Both of these map the comma-delimited I tried removing the Splitting from the mapper and it created the same� A convention-based object-object mapper in .NET. . Contribute to AutoMapper/AutoMapper development by creating an account on GitHub.

As error says, Split function has an optional parameter. The full signature of it is as this (options is optional)

public string[] Split(string separator, StringSplitOptions options = StringSplitOptions.None)

As you are trying to use a function with default value inside an expression tree, it gives you the error. To Fix it, easy, just pass on optional parameters by yourself. ( StringSplitOptions.None ) So, simply change it to this:

entity.Keywords.Split(',' , StringSplitOptions.None).ToList()

Get a string from an array on mapping � Issue #2699 � AutoMapper , I have the following configuration on the Automapper 6 CreateMap () . MoveNext() --- End of stack trace from previous location where� AutoMapper is a simple little library built to solve a deceptively complex problem - getting rid of code that mapped one object to another. This type of code is rather dreary and boring to write, so why not invent a tool to do it for us?

I had the same problem. I do not know if it is an issue or not. Anyway, I found a workaround.

 CreateMap<Category, GetCategoryRest>()
                .ForMember(dest => dest.Words, 
                    opt => opt.MapFrom(src => ToWordsList(src.Words)));

 private static List<string> ToWordsList(string words)
   return string.IsNullOrWhiteSpace(words) ? new List<string>() : words.Split(",").ToList();

It is guaranteed that AutoMapper has always a List. Still, I'm confused. In my Startup.cs I define that AutoMapper allows null values for list.

Mapper.Initialize(cfg => {
 cfg.AllowNullCollections = true;

Category.Words is a string. GetCategoryRest.Words is a List<string>

AutoMapper Version: 8.1.1, AutoMapper.Microsoft.DependencyInjection: 6.1.1

AutoMapper Complex Mapping in C#, C# String in Depth Splitting Tuples in C# Let us understand the AutoMapper Complex Mapping with an example. static Mapper InitializeAutomapper() In order to solve the above problem, you need to configure the mapping between� For the analogous ICollections in the view models I use Lists instead. When I want to create/edit entities I map a model to the entity as shown in this gist . In previous versions the HashSet would be replaced a List and the database operation would proceed without issue, but since 6.2 you get an AutoMapperMappingException.

Custom Type Converters — AutoMapper documentation, [Test] public void Example() { var configuration = new MapperConfiguration(cfg = > { cfg.CreateMap<string, int>().ConvertUsing(s => Convert.ToInt32(s)); cfg. For those using Catel.IoC here is how you register AutoMapper. First define the configuration using profiles. And then you let AutoMapper know in what assemblies those profiles are defined by registering AutoMapper in the ServiceLocator at startup:

automapper - 在AutoMapper问题中使用string.Split(), 我只是想让我的AutoMapper配置为按照此配置将以逗号分隔的字符串转换 Split( ) Using string.Split() in AutoMapper issue. 发表于 2019-02-25� A few months ago, Jimmy Bogard, author of the excellent AutoMapper wrote a great article about Autoprojecting LINQ queries. Now that Jimmy has done all the hard expression tree work, this article extends his example to include caching and simple flattening capabilities and goes on to show it in use in a simple EF 4.1 Code First application.

AutoMapper will ignore null reference exceptions when mapping your source to your target. This is by design. If you don’t like this approach, you can combine AutoMapper’s approach with custom value resolvers if needed. Once you have your types you can create a map for the two types using a MapperConfiguration and CreateMap.

  • That works for me. Try upgrading AM. A repro would help. Make a gist that we can execute and see fail.