These are my domain and view classes:
public abstract class Entity : IEntity
{
[Key]
public virtual int Id { get; set; }
}
public class City:Entity
{
public string Code { get; set; }
}
public class BaseViewModel:IBaseViewModel
{
public int Id { get; set; }
}
public class CityModel:BaseViewModel
{
public string Code { get; set; }
}
This is my mapping extension:
public static TModel ToModel<TModel>(this IEntity entity)
where TModel : IBaseViewModel
{
return (TModel)Mapper.Map(entity, entity.GetType(), typeof(TModel));
}
This is how it’s used:
City city = GetCity();
CityModel model = city.ToModel<CityModel>();
but for generic lists it doesn’t work:
List<City> cities = GetCities();
List<CityModel> model =cities.ToModel<CityModel>() // doesn't work
Is it possible to write an extension method for a generic list?
You will need something like this