AutoMapper and Sorting the child collection conditionally












1














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?










share|improve this question




















  • 1




    Have you tried using ForMember() to map the bar property as you wish? You could apply your sorting there.
    – RedFox
    Nov 12 at 7:15










  • Do you have the SortOrder property in your Bar object, too?
    – RedFox
    Nov 12 at 7:24










  • @RedFox Yes, SortOrder is present in the Bar object, but Name is not in the Bar object. Please see my edits.
    – 9_A
    Nov 12 at 7:40
















1














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?










share|improve this question




















  • 1




    Have you tried using ForMember() to map the bar property as you wish? You could apply your sorting there.
    – RedFox
    Nov 12 at 7:15










  • Do you have the SortOrder property in your Bar object, too?
    – RedFox
    Nov 12 at 7:24










  • @RedFox Yes, SortOrder is present in the Bar object, but Name is not in the Bar object. Please see my edits.
    – 9_A
    Nov 12 at 7:40














1












1








1







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?










share|improve this question















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?







c# sorting nhibernate fluent-nhibernate automapper






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 7:39

























asked Nov 12 at 6:53









9_A

135




135








  • 1




    Have you tried using ForMember() to map the bar property as you wish? You could apply your sorting there.
    – RedFox
    Nov 12 at 7:15










  • Do you have the SortOrder property in your Bar object, too?
    – RedFox
    Nov 12 at 7:24










  • @RedFox Yes, SortOrder is present in the Bar object, but Name is not in the Bar object. Please see my edits.
    – 9_A
    Nov 12 at 7:40














  • 1




    Have you tried using ForMember() to map the bar property as you wish? You could apply your sorting there.
    – RedFox
    Nov 12 at 7:15










  • Do you have the SortOrder property in your Bar object, too?
    – RedFox
    Nov 12 at 7:24










  • @RedFox Yes, SortOrder is present in the Bar object, but Name is not in the Bar object. Please see my edits.
    – 9_A
    Nov 12 at 7:40








1




1




Have you tried using ForMember() to map the bar property as you wish? You could apply your sorting there.
– RedFox
Nov 12 at 7:15




Have you tried using ForMember() to map the bar property as you wish? You could apply your sorting there.
– RedFox
Nov 12 at 7:15












Do you have the SortOrder property in your Bar object, too?
– RedFox
Nov 12 at 7:24




Do you have the SortOrder property in your Bar object, too?
– RedFox
Nov 12 at 7:24












@RedFox Yes, SortOrder is present in the Bar object, but Name is not in the Bar object. Please see my edits.
– 9_A
Nov 12 at 7:40




@RedFox Yes, SortOrder is present in the Bar object, but Name is not in the Bar object. Please see my edits.
– 9_A
Nov 12 at 7:40












1 Answer
1






active

oldest

votes


















1














Since one of the properties you want to use for the order is only available after you mapped the data you can use the AfterMap() functionality of AutoMapper like this:



mapper.CreateMap<Foo, FooDto>()
.AfterMap(
(foo, dto) =>
{
dto.Bars = foo.SortBarByName
? dto.Bars.OrderBy(x => x.Name)
: dto.Bars.OrderBy(x => x.SortOrder);
});


This should order the Bars after all data is available.






share|improve this answer





















  • It worked. Thanks.
    – 9_A
    Nov 12 at 9:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257167%2fautomapper-and-sorting-the-child-collection-conditionally%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Since one of the properties you want to use for the order is only available after you mapped the data you can use the AfterMap() functionality of AutoMapper like this:



mapper.CreateMap<Foo, FooDto>()
.AfterMap(
(foo, dto) =>
{
dto.Bars = foo.SortBarByName
? dto.Bars.OrderBy(x => x.Name)
: dto.Bars.OrderBy(x => x.SortOrder);
});


This should order the Bars after all data is available.






share|improve this answer





















  • It worked. Thanks.
    – 9_A
    Nov 12 at 9:19
















1














Since one of the properties you want to use for the order is only available after you mapped the data you can use the AfterMap() functionality of AutoMapper like this:



mapper.CreateMap<Foo, FooDto>()
.AfterMap(
(foo, dto) =>
{
dto.Bars = foo.SortBarByName
? dto.Bars.OrderBy(x => x.Name)
: dto.Bars.OrderBy(x => x.SortOrder);
});


This should order the Bars after all data is available.






share|improve this answer





















  • It worked. Thanks.
    – 9_A
    Nov 12 at 9:19














1












1








1






Since one of the properties you want to use for the order is only available after you mapped the data you can use the AfterMap() functionality of AutoMapper like this:



mapper.CreateMap<Foo, FooDto>()
.AfterMap(
(foo, dto) =>
{
dto.Bars = foo.SortBarByName
? dto.Bars.OrderBy(x => x.Name)
: dto.Bars.OrderBy(x => x.SortOrder);
});


This should order the Bars after all data is available.






share|improve this answer












Since one of the properties you want to use for the order is only available after you mapped the data you can use the AfterMap() functionality of AutoMapper like this:



mapper.CreateMap<Foo, FooDto>()
.AfterMap(
(foo, dto) =>
{
dto.Bars = foo.SortBarByName
? dto.Bars.OrderBy(x => x.Name)
: dto.Bars.OrderBy(x => x.SortOrder);
});


This should order the Bars after all data is available.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 7:49









RedFox

1259




1259












  • It worked. Thanks.
    – 9_A
    Nov 12 at 9:19


















  • It worked. Thanks.
    – 9_A
    Nov 12 at 9:19
















It worked. Thanks.
– 9_A
Nov 12 at 9:19




It worked. Thanks.
– 9_A
Nov 12 at 9:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53257167%2fautomapper-and-sorting-the-child-collection-conditionally%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues