0

I have this class:

public class Presenter
{
    public int? Society { get; set; }

    public int? SenderCode { get; set; }

    public string BusinessNameSender { get; set; }

    public string AddressSender { get; set; }

    public string VatNumberSender { get; set; }

    public string Check { get; set; }   
}

Retrieve the data with a query and with the ExtractField method populate the Presenter class

public static IEnumerable<Presenter> ExtractField(IEnumerable<dynamic> query)
{
    return query.Select(x => new Presenter()
    {
        Society = x.ids.Society ?? null,
        SenderCode = x.ids.SenderCode ?? null,
        BusinessNameSender = x.details.Sender,
        AddressSender = x.details.DestinationAddress,
        VatNumberSender = x.details.VatNumberSender,
        Check = x.ids.Check,
    });
}

When I then use the ToList() to then display the result on the screen within the list, the fields are sorted alphabetically, how do I make sure that the fields are seen in the order I have set?

I used dynamic because having two distinct queries but which have the same select seemed to me the best way not to duplicate code

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Riccardo Pezzolati
  • 357
  • 1
  • 4
  • 15
  • Where do you display it? Console? How are you outputing it? – vsarunov Aug 24 '21 at 09:05
  • I display the result in a json (the application is a Rest API) – Riccardo Pezzolati Aug 24 '21 at 09:09
  • 1
    Have you looked at this https://stackoverflow.com/questions/3330989/order-of-serialized-fields-using-json-net ? – vsarunov Aug 24 '21 at 09:10
  • 1
    Debugger always show Properties/members alphabetically. json serializing should maintain the order, how the class is coded – dba Aug 24 '21 at 09:11
  • If you are using Json.NET to serialize data then you can take advantage of [JsonPropertyAttribute's Order property](https://www.newtonsoft.com/json/help/html/JsonPropertyOrder.htm) to define custom ordering. – Peter Csala Aug 24 '21 at 09:17
  • I don't think he wants to use custom order, he wants it the way, the class is defined but is confused by the debugger always sorting alphabetically... but maybe I misunderstood him :) – dba Aug 24 '21 at 09:20

0 Answers0