简体   繁体   中英

C# List - Sorting Data

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; }
}

You can use Enumerable.OrderBy extension for enumerating your collection in some particular order (eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM