2014-10-02 78 views
0

我正在写C#排序列表框与多个字段按日期

我有两个列表框。

列表框1被填充,我没有问题。

然后,我有一个foreach循环遍历列表框1中的每个项目,并基于linq查询填充列表框2。

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

        } 

所以现在我的列表框2看起来是这样的

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

我怎么会那么解决这第二个列表框中海湾日期下降到这个样子?

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

您可以在日期存储在一个排序的字典,然后检索值使用keyvaluepair保存,并把它们添加到列表框中 – deathismyfriend 2014-10-02 20:36:20

回答

1

首先获得日期的顺序列表,然后填充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); 
} 
+0

System.Windows.Forms.ListBox.ObjectCollection不包含select的定义,就是这个错误。 – Zingo 2014-10-02 20:47:20

+0

添加:使用System.Linq – 2014-10-02 20:51:17

+0

我已经有那里的参考 – Zingo 2014-10-02 20:55:14