2012-01-31 39 views
2

我有2个数据集,如下图所示,我需要这些数据集合并成1:如何排序和顺序合并数据集

数据集1

date    reason  total 
12 aug 2010  inactive 123 
19 aug 2010  inactive 45 
20 sep 2010  inactive 145 
02 nov 2010  inactive 95 
25 dec 2010  inactive 44 

dataset2

date    reason  total 
12 aug 2010  active 12 
21 aug 2010  active 45 
20 sep 2010  active 45 
02 nov 2010  active 45 
26 dec 2010  active 45 

我可以合并通过使用Merge方法的数据集,但我怎么对数据集进行排序,得到的结果是这样的:

date    reason  total 
12 aug 2010  inactive  123 
12 aug 2010  active  12 
19 aug 2010  inactive  45 
21 aug 2010  active  45 
20 sep 2010  inactive 145 
20 sep 2010  active  45 
02 nov 2010  inactive 95 
02 nov 2010  active  45 
25 dec 2010  inactive 44 
26 dec 2010  active  45 
+0

什么是日期列的数据类型?文字或日期?如果是日期请参阅http://stackoverflow.com/questions/513961/c-sharp-how-do-i-sort-a-datatable-by-date – 2012-01-31 06:24:03

回答

4

假设你想在升序日期顺序及原因从大到小的顺序,你可以使用一个DataView的Sort属性(DataView.Sort Property):

// Assuming the merged table is the first and only table in the DataSet. 
DataView dv = new DataView(dataSet1.Tables[0]); 

dv.Sort = "date, reason DESC"; 

我没有测试过这一点 - 就在我的头顶。应该指出你在正确的方向。

+0

工作良好蒂姆谢谢 – happysmile 2012-02-02 02:20:10