1

I am trying to add elements into a list, order them and then output them, there a number of "columns" if you like, per list

List<Info> infoList = new List<Info>();

while (dr.Read())
{

   meeting_id = dr.GetValue(0).ToString();
   try
   {
      Appointment appointment = Appointment.Bind(service, new ItemId(meeting_id));

      Info data = new Info();
      data.Start = appointment.Start;
      data.Fruit = Convert.ToInt32(dr.GetValue(1));
      data.Nuts = Convert.ToInt32(dr.GetValue(2));

      infoList.Add(data);

   }

Then to output it I want to order it by Start and then display all associated columns

 for (int i = 0; i < infoList.Count; i++)
 {
     meet = meet + infoList[i];
 }   

First question: is the way I am inputting the data right?

Second question: How to I output all the columns to display all the associated columns? Is this possible? Is there a better practice?

Thanks

EDIT:

The class if you are interested:

public class Info
{
    public DateTime Start { get; set; }
    public int Fruit { get; set; }
    public int Nuts { get; set; }
}

1 Answer 1

3

You can use Enumerable.OrderBy extension for enumerating your collection in some particular order (e.g. ordered by Start property value):

foreach(var info in infoList.OrderBy(i => i.Start))
{
    // use info object here
    // info.Fruits
    // info.Nuts
}

BTW consider to add sorting on database side - that will be more efficient

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, the reason I can't sort database side is that no start date is pulled from the database. The only thing stored in the database is the meeting ID (from Outlook), the "fruit" and the "nuts" and then I use EWS to pull in the start time from Exchange.
@CorbinSpicer I see. Then you have only option to sort collection in memory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.