如何从二维数组中填充一维数组,以及如何对一维数组进行排序?

问题描述:

hanks大家来帮助我的白痴问题(看我以前的帖子:))。但我确实需要下面。我应该填写每个单元Datatable的长度的一个二维数组。并对1D阵列linq进行排序并且也不用linq如何从二维数组中填充一维数组,以及如何对一维数组进行排序?

int[][] lengths; 

      using (DataTable table = GetTable()) 
      { 
       lengths = (from DataRow row in table.Rows 
          select 
          (from DataColumn col in table.Columns 
          select row[col].ToString().Length).ToArray()).ToArray(); 
      } 

      int[] Sortedlist; 
      foreach (int[] row in lengths) 
      { 
       Sortedlist = row; ---- I NEED HELP !!!! 
      } 

      foreach (int item in Sortedlist) 
      { 
       item.Sort(); ----- I NEED HELP!!! 
      } 

My Data:

static DataTable GetTable() { // // Here we create a DataTable with four columns. // DataTable table = new DataTable(); table.Columns.Add("Dosage", typeof(int)); table.Columns.Add("Drug", typeof(string)); table.Columns.Add("Patient", typeof(string)); table.Columns.Add("Date", typeof(DateTime)); // // Here we add five DataRows. // table.Rows.Add(25, "Indocin", "David", DateTime.Now); table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now); table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now); table.Rows.Add(21, "Combivent", "Janet", DateTime.Now); table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now); return table; }

不确定你的意思。你有一个int [] []数据结构(来自主linq查询)。 您的需求是将其转换为int []结构,然后使用linq对其进行排序,并使用Array.sort对其进行排序。 这应该这样做

int[] UnSortedlist = lengths.SelectMany(x => x).ToArray(); 
      int[] sortedListLinq = UnSortedlist.OrderBy(x => x).ToArray(); 
      Array.Sort(UnSortedlist); 
      int[] sortedListnonLinq = UnSortedlist; 
+0

我喜欢:Array.Sort(UnSortedlist); int [] sortedListnonLinq = UnSortedlist; 但我怎么能使用降序像Reverse()方法? – Penguen 2010-06-20 19:41:42