public string FirstName { get; set; } public string LastName { get; set; } public int ID { get; set; } public GradeLevel? Year { get; set; } public List ExamScores { get; set; } public Student(string FirstName, string LastName, int ID, GradeLevel Year, List ExamScores) this.FirstName = FirstName; this.LastName = LastName; this.ID = ID; this.Year = Year; this.ExamScores = ExamScores; public Student(string FirstName, string LastName, int StudentID, List? ExamScores = null) this.FirstName = FirstName; this.LastName = LastName; ID = StudentID; this.ExamScores = ExamScores ?? Enumerable.Empty().ToList(); public static List students = new() FirstName: "Terry", LastName: "Adams", ID: 120, Year: GradeLevel.SecondYear, ExamScores: new() { 99, 82, 81, 79 } "Fadi", "Fakhouri", 116, GradeLevel.ThirdYear, new() { 99, 86, 90, 94 } "Hanying", "Feng", 117, GradeLevel.FirstYear, new() { 93, 92, 80, 87 } "Cesar", "Garcia", 114, GradeLevel.FourthYear, new() { 97, 89, 85, 82 } "Debra", "Garcia", 115, GradeLevel.ThirdYear, new() { 35, 72, 91, 70 } "Hugo", "Garcia", 118, GradeLevel.SecondYear, new() { 92, 90, 83, 78 } "Sven", "Mortensen", 113, GradeLevel.FirstYear, new() { 88, 94, 65, 91 } "Claire", "O'Donnell", 112, GradeLevel.FourthYear, new() { 75, 84, 91, 39 } "Svetlana", "Omelchenko", 111, GradeLevel.SecondYear, new() { 97, 92, 81, 60 } "Lance", "Tucker", 119, GradeLevel.ThirdYear, new() { 68, 79, 88, 92 } "Michael", "Tucker", 122, GradeLevel.FirstYear, new() { 94, 92, 91, 91 } "Eugene", "Zabokritski", 121, GradeLevel.FourthYear, new() { 96, 85, 91, 60 } enum GradeLevel FirstYear = 1, SecondYear, ThirdYear, FourthYear

以下查询将返回在第一堂考试中得分为 90 或更高的学生。

void QueryHighScores(int exam, int score) var highScores = from student in students where student.ExamScores[exam] > score select new Name = student.FirstName, Score = student.ExamScores[exam] foreach (var item in highScores) Console.WriteLine($"{item.Name,-15}{item.Score}"); QueryHighScores(1, 90);

该查询有意简单,以使你可以进行实验。 例如,你可以在 where 子句中尝试其他条件或使用 orderby 子句对结果进行排序。

  • 语言集成查询 (LINQ)
  • 字符串内插
  •