2010-06-20 110 views
-1

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

回答

0

不確定你的意思。你有一個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