对DataTable的DataRow做group

   1:  public void DataSetLinq41()
   2:  {
   3:      var words4 = testDS.Tables["Words4"].AsEnumerable();
   5:      var wordGroups =
   6:          from w in words4
   7:          group w by w.Field<string>("word")[0] into g
   8:          select new { FirstLetter = g.Key, Words = g };
  10:      foreach (var g in wordGroups)
  11:      {
  12:          Console.WriteLine("Words that start with the letter '{0}':", g.FirstLetter);
  13:          foreach (var w in g.Words)
  14:          {
  15:              Console.WriteLine(w.Field<string>("word"));
  16:          }
  17:      }
  18:      Console.ReadLine();
  19:  }
  20:   
  21:   
  22:  private static DataTable CreateWords4Table()
  23:  {
  24:      string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" };
  25:      DataTable table = new DataTable("Words4");
  26:      table.Columns.Add("word", typeof(string));
  27:   
  28:      foreach (string word in words)
  29:      {
  30:          table.Rows.Add(new object[] { word });
  31:      }
  32:      return table;
  33:  }
  34:   
  35:  DataSetLinq41();
原文地址:https://www.cnblogs.com/pnljs/p/2933672.html