Using mapping select and order by

The code below works really well, but its missing an order by which in the table Client_Name the field is called Display_Order, I cant work out how to build it into the line of code below.

.ForMember(dest => dest.ClientName, opt => opt.MapFrom(src => string.Join(",", src.Client_Name.Select(record => record.Name))))

Ive got this far, but I got a red squiggly line under the Display_Order bit after the a. of a.Display_Order

.ForMember(dest => dest.ClientName, opt => opt.MapFrom(src => string.Join(",", src.Client_Name.Select(record => record.Name).OrderBy(a => a.Display_Order))))

And in that field the values are 1, 2, 3, 4 etc basically its a numbering order by

If someone is feeling like helping a bit more too, ideally the comma should be replaced with a ‘< br />’ and each ‘record.Name’ is outputted on a new line, so they stack in order vertically, does that make sense. Basically each one is ordered by numbers starting at 1 at the top and then each new record displayed on a new line vertically

OK I had them the wrong way around, this works well

.ForMember(dest => dest.ClientName, opt => opt.MapFrom(src => string.Join("<br/>", src.Client_Name.OrderBy(record => record.Display_Order).Select(record => record.Name))))

So next thing is how to remove the comma seperator and use a ‘< br />’ instead as above. But it just outputs the ‘
’ as a string rather than as html

This is pushing the boat out now, but would there also be a way of either making the first one show in bold, and the others as normal, or from number 2 on they are a slightly smaller font that the first one.

OK I got to the problem, in that I needed to use

@Html.Raw(record.ClientName)

For the ‘<br’> to work, but we dont like using html.raw as these details are inputed manually via a form, so someone could also input jscript in with the name

Try HTMLEncode on the record.ClientName before you substitute for <br/>

See HttpServerUtility.HtmlEncode Method (String) for more information.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.