简体   繁体   中英

Map a linq or a query of EF to a ViewModel using automapper

I´m using Visual Studio 2019 and .net core 3.1. I have a ViewModel to show on the views the regions with their country, the name of the country, not the Id, the foreign key. A country have several regions.

I´ve tried with linq and a query of EF but I can´t get the mapping wright.

public IActionResult Index()
{            
    var data = (from r in _context.CustomerRegions
                join c in _context.CustomerCountries
                on r.IdCustomerCountry equals c.IdCustomerCountry
                select new
                {
                    r.IdCustomerRegion,
                    r.CustomerRegion,
                    c.IdCustomerCountry,
                    c.CustomerCountryName   
                }).OrderBy(m => m.CustomerCountryName);            

    var data2 = _context.CustomerRegions
        .Include("CustomerContry.CustomerCountryName").FirstOrDefault();

    List<CustomerCountryRegionVM> regionsWithCountries = _mapper
        .Map<List<CustomerRegions>, List<CustomerCountryRegionVM>>(data2);

    ...
}   

data2. You can´t convert from CustomerRegions to Generic list CustomerCountryRegionVM

data. You can´t convert from Order Iqueryable to generic list

On mapping class:

CreateMap<CustomerCountryRegionVM, CustomerRegions>();
CreateMap<CustomerRegions, CustomerCountryRegionVM>()
    .ForMember(dest => dest.CustomerCountryName, opt => 
        opt.MapFrom(src => src.CustomerCountry));

The viewmodel:

public class CustomerCountryRegionVM
{
    public int IdCustomerRegion { get; set; }

    public string CustomerRegion { get; set; }

    public int IdCustomerCountry { get; set; }        
    
    public string CustomerCountryName { get; set; }
}

The model:

public class CustomerRegions
{
    [Key]
    public int IdCustomerRegion { get; set; }

    [StringLength(50, ErrorMessage = "Longitud máxima para la región: 50")]
    public string CustomerRegion { get; set; }

    [ForeignKey("IdCustomerCountry")]
    public int IdCustomerCountry { get; set; }
    public CustomerCountries CustomerCountry { get; set; }
    
    public ICollection<CustomerCities> CustomerCities { get; set; }
}

****************************update ****************

    var configuration = new MapperConfiguration(cfg => cfg.CreateMap<CustomerCountries, CustomerCountryRegionVM>()
.ForMember(dto => dto.CustomerRegion, conf => conf.MapFrom(ol => ol.CustomerRegions))); 
//I can´t see the next field on the intellicense
    
    public List<CustomerCountryRegionVM> GetLinesForOrder(int orderId)
            {
                using (var context = new orderEntities())
                {
                    return context.OrderLines.Where(ol => ol.OrderId == orderId)
                             .ProjectTo<CustomerCountryRegionVM>(configuration).ToList();
                }
            }

After many days I´ve found the solution

CreateMap<CustomerRegions, CustomerCountryRegionVM>()

                .ForMember(x => x.CustomerCountryName, opt => opt.MapFrom(z => z.CustomerCountry.CustomerCountryName));

On the controller

var customerRegions = await _context.CustomerRegions
                .Include(c=>c.CustomerCountry)
                .ToListAsync();


List<CustomerCountryRegionVM> regions = _mapper.Map<List<CustomerRegions>,
                                                    List<CustomerCountryRegionVM>>(customerRegions);

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