You can do this
CreateMap<Country, CountryDTO>().ForMember(x => x.Country, opt => opt.MapFrom(src => string.IsNullOrEmpty(src.CountryEnglishName) ? src.CountryArabicName : src.CountryEnglishName));
Here is an example where you can use a customer resolver that can hold the api parameter
using System;
using AutoMapper;
using Microsoft.Extensions.DependencyInjection;
namespace ConsoleApp7
{
class Country
{
public int id { get; set; }
public string CountryEnglishName { get; set; }
public string CountryArabicName { get; set; }
}
class CountryDTO
{
public int id { get; set; }
public string Country { get; set; }
}
public interface IX
{
string ApiParameter { get; }
}
public class X : IX
{
// Here is where you get the parameter from the request if you need it from let's say the HttpContext, change this and you will see a differnt output
public string ApiParameter => "English";
}
public class CustomResolver : IMemberValueResolver<object, object, string, string>
{
private readonly string _parameter;
public CustomResolver(IX x)
{
_parameter = x.ApiParameter;
}
public string Resolve(object source, object destination, string sourceMember, string destinationMember, ResolutionContext context)
{
var c = (Country) source;
return _parameter == "English" ? c.CountryEnglishName : c.CountryArabicName;
}
}
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<Country, CountryDTO>()
.ForMember(x => x.Country, opt => opt.MapFrom<CustomResolver,string>(src=>src.CountryEnglishName));
}
}
class Program
{
static void Main(string[] args)
{
var services = new ServiceCollection();
services.AddAutoMapper(typeof(MappingProfile));
services.AddScoped<IX, X>();
var mapper = services.BuildServiceProvider().GetRequiredService<IMapper>();
var c = new Country()
{
CountryEnglishName = "A",
CountryArabicName = "B"
};
var dto = mapper.Map<Country, CountryDTO>(c);
Console.WriteLine(dto.Country);
}
}
}