MST

星途 面试题库

面试题:C# LINQ查询表达式中等难度题

假设有一个包含学生信息的List<Student>,Student类有属性Name(姓名)、Age(年龄)和Score(成绩)。请使用C# LINQ查询表达式找出年龄大于18岁且成绩大于80分的学生姓名,并将结果按成绩从高到低排序。
30.0万 热度难度
编程语言C#

知识考点

AI 面试

面试题答案

一键面试
using System;
using System.Collections.Generic;
using System.Linq;

class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
    public int Score { get; set; }
}

class Program
{
    static void Main()
    {
        List<Student> students = new List<Student>()
        {
            new Student { Name = "Alice", Age = 20, Score = 85 },
            new Student { Name = "Bob", Age = 17, Score = 78 },
            new Student { Name = "Charlie", Age = 19, Score = 90 },
            new Student { Name = "David", Age = 22, Score = 82 }
        };

        var result = from student in students
                     where student.Age > 18 && student.Score > 80
                     orderby student.Score descending
                     select student.Name;

        foreach (var name in result)
        {
            Console.WriteLine(name);
        }
    }
}