简体   繁体   中英

Map Entity Framework collection to comma delimted string with automapper

I have a parent class:

public class Parent
{
    ...
    public List<Location> Locations { get; set; }
}

Location class:

public class Location
{
    public int LocationId { get; set; }
    public string Name { get; set; }
}

Destination class for mapping:

public class Destination
{
    ...
    public string DelimitedLocations { get; set; }
}

I need to map the LocationId list from Locations to a comma delimited string using automapper.

Here are several things I have tried:

CreateMap<Parent, Destination>().ForMember(d => d.DelimitedLocations , o => o.MapFrom(s => string.Join(",", s.Locations.ToList().Select(t => t.LocationID.ToString()))))

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

Next attempt:

CreateMap<Parent, Destination>()..ForMember(d => d.TestPlotLocationsSelected, o => o.MapFrom(s => s.TestPlotLocations.ToList().Select(t => string.Join(",", t.TestPlotLocationID.ToString()))))

Result: No method 'ToString' exists on type 'System.Collections.Generic.IEnumerable`1[System.String]'.

Not sure what to try next.

Select statement should be something like

o.Locations.Select(x => x.LocationId).ToList()

Demo

public class Program
{
    public static void Main()
    {
        Initialize();

        var source = new Parent
        {
            Locations = new List<Location>
            {
                new Location {LocationId = 1, Name = "One"},
                new Location {LocationId = 2, Name = "Two"},
                new Location {LocationId = 3, Name = "Three"},
            }
        };

        var destination = Mapper.Map<Parent, Destination>(source);

        Console.ReadLine();
    }

    public static void Initialize()
    {
        MapperConfiguration = new MapperConfiguration(cfg =>
        {
            cfg.CreateMap<Parent, Destination>()
                .ForMember(dest => dest.DelimitedLocations, mo => mo.MapFrom(src =>
                    src.Locations != null
                        ? string.Join(",", src.Locations.Select(x => x.LocationId).ToList())
                        : ""));
        });
        Mapper = MapperConfiguration.CreateMapper();
    }

    public static IMapper Mapper { get; private set; }

    public static MapperConfiguration MapperConfiguration { get; private set; }
}

public class Parent
{
    public List<Location> Locations { get; set; }
}

public class Location
{
    public int LocationId { get; set; }
    public string Name { get; set; }
}

public class Destination
{
    public string DelimitedLocations { get; set; }
}

Result

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM