I have an object graph that I'm loading from a database using Fluent NHibernate and AutoMapper into DTOs:-
public class Foo
{
public int Id { get; set; }
public virtual ICollection<Bar> Bars { get; set; }
public bool SortBarByName
}
public class Bar
{
public int Id { get; set; }
public int SortOrder { get; set; }
public virtual Foo Foo { get; set; }
}
My mappings look like:-
public class FooDto
{
public IEnumerable<BarDto> Bars { get; set; }
}
public class BarDto
{
public string Name { get; set; }
public int SortOrder { get; set; }
}
My mapping looks like:
mapper.CreateMap<Foo, FooDto>().ForMember(d => d.Bars, o => o.MapFrom(s => s.Bars));
mapper.CreateMap<Bar, BarDto>();
When I return FooDto, I want collection of BarDto to be sorted based on below condition. I want to sort BarDto by Name if SortBarByName is true or by SortOrder if SortBarByName is false, based on SortBarByName property of class Foo.
Note: Name property is not present in Bar class, it is only in BarDto.
Can I do something in the AutoMappers to achieve this?