1.按照第一个字母进行分组
static void Main() { string[] words = { "blueberry", "chimpanzee", "abacus", "banana", "apple", "cheese" }; var wordGroupByFirstLetter = from word in words group word by word[0] into w orderby w.Key select w; foreach (var groupedWord in wordGroupByFirstLetter) { Console.WriteLine("word that start with letter is {0}",groupedWord.Key); foreach (var word in groupedWord) { Console.WriteLine("{0}", word); } } }2.按照平均分大于80的
public class Student { public string First { get; set; } public string Last { get; set; } public int ID { get; set; } public List<int> Scores; } class Program { public static List<Student> GetStudents() { List<Student> students = new List<Student> { new Student {First="Svetlana", Last="Omelchenko", ID=111, Scores= new List<int> {97, 72, 81, 60}}, new Student {First="Claire", Last="O'Donnell", ID=112, Scores= new List<int> {75, 84, 91, 39}}, new Student {First="Sven", Last="Mortensen", ID=113, Scores= new List<int> {99, 89, 91, 95}}, new Student {First="Cesar", Last="Garcia", ID=114, Scores= new List<int> {72, 81, 65, 84}}, new Student {First="Debra", Last="Garcia", ID=115, Scores= new List<int> {97, 89, 85, 82}} }; return students; } static void Main(string[] args) { List<Student> students = GetStudents(); var boolenGroupQuery = from student in students group student by student.Scores.Average() > 80; foreach (var boolAverage in boolenGroupQuery) { Console.WriteLine("{0}", boolAverage.Key == true ? "High Average" : "Low Average"); foreach (var student in boolAverage) { Console.WriteLine("First:{0},last:{1},Average:{2}", student.First, student.Last, student.Scores.Average()); } } }