Zingo
Zingo

Reputation: 291

Sorting ListBox With Multiple Fields By Date

I'm writing in C#

I have two list boxes.

List Box 1 gets populated and i have no problems with that.

I then have a foreach loop that goes through each item in List Box 1 and populates list box 2 based off of a linq query.

                    foreach (var item in listBox2.Items) 
                    {

                      var date =
                           (from z in task.tblContractProcessRequests
                            where z.RequestID == Int32.Parse(item.ToString()) && z.RequestTypeID == 1
                            select z).Single();

                      listBox3.Items.Add( date.RequestID + " - " + date.RequestTime);

                    }

So now my List box 2 looks something like this

 12345 - 01/12/2010
 65432 - 03/12/2009
 92354 - 12/31/2013

How would i then sort this second list box bay date descending to look like this?

 92354 - 12/31/2013
 12345 - 01/12/2010
 65432 - 03/12/2009

Upvotes: 0

Views: 127

Answers (1)

Dave Mackersie
Dave Mackersie

Reputation: 1061

First get an ordered list of dates, then populate listBox3:

var dates = listBox2.Items.Cast<object>().Select( item => 
     (from z in task.tblContractProcessRequests
      where z.RequestID == Int32.Parse(item.ToString()) && z.RequestTypeID == 1
      select z).Single() ).OrderByDescending(d => d.RequestTime);


 foreach (var date in dates) 
 {
    listBox3.Items.Add( date.RequestID + " - " + date.RequestTime);
 }

Upvotes: 1

Related Questions