0
public class Team {
 public string Flag
}

public class TeamDto {
 public char? Flag
}

using Automapper, how can I map Flag string to Flag char?

Mapper config:

 cfg.CreateMap<Team, TeamDto>()
                .ForMember(x=>x.Flag, opts => opts.MapFrom(src => src.Flag!= null ? src.Flag[0] : (char?) null))

I am getting the error when running this. using EF6 DbContext

dbContext.Teams.ProjectTo<TeamDto>(mapper.ConfigurationProvider).ToList();

This is the error

System.NotSupportedException: 'LINQ to Entities does not recognize the method 'Char get_Chars(Int32)' method, and this method cannot be translated into a store expression.'

jdidi
  • 159
  • 1
  • 4
  • 15
  • Does `src.Flag.Substring(0, 1)` work where `src.Flag[0]` does not? – D M Oct 20 '21 at 16:43
  • @DM does not work as it should return a char, src.Flag.Substring(0, 1) returns a string – jdidi Oct 20 '21 at 16:46
  • Right, right, my bad. It looks like EF doesn't have access to iterate the string's `char` array. You likely won't have to worry about non-ASCII characters in your flags, right? – D M Oct 20 '21 at 16:47
  • @DM yes I don't – jdidi Oct 20 '21 at 16:49
  • I'd try just casting the result: `(char?)src.Flags.Substring(0, 1)`. – D M Oct 20 '21 at 16:50
  • @DM I am getting Error: Cannot convert type 'string' to 'char' – jdidi Oct 20 '21 at 16:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/238368/discussion-between-jdidi-and-d-m). – jdidi Oct 20 '21 at 16:54
  • The answer is: you can't. In ef-core there (finally) are value conversions. See also https://stackoverflow.com/a/6762719/861716. – Gert Arnold Oct 20 '21 at 19:02

0 Answers0