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()); } } }
本文展示了如何使用C#编程语言通过分组操作按首字母对单词数组进行排序,并通过条件筛选出平均成绩高于80分的学生信息。
1340

被折叠的 条评论
为什么被折叠?



