Linq练习

准备类以及数据 

class Student
{
    public int StudentId { get; set; }
    public string Name { get; set; }
    public Course Course { get; set; }

    public Student(int studentId, string name, Course course)
    {
        StudentId = studentId;
        Name = name;
        Course = course;
    }

    public void PrintInfo()
    {
        Console.WriteLine("Student ID: " + StudentId);
        Console.WriteLine("Name: " + Name);
        Console.WriteLine("Current Course: " + Course.Name);
        Console.WriteLine("Course Instructor: " + Course.Instructor);
    }
}

class Course
{
    public int CourseId { get; set; }
    public string Name { get; set; }
    public string Instructor { get; set; }

    public Course(int courseId, string name, string instructor)
    {
        CourseId = courseId;
        Name = name;
        Instructor = instructor;
    }
}
 List<Student> studentList = new List<Student>()
            {
                new Student(10001, "Alice", new Course(1, "Mathematics", "John Smith")),
                new Student(10002, "Bob", new Course(2, "Computer Science", "Jane Doe")),
                new Student(10003, "Charlie", new Course(3, "History", "Susan Johnson")),
                new Student(10004, "David", new Course(1, "Mathematics", "John Smith")),
                new Student(10005, "Eva", new Course(4, "Physics", "Robert Brown")),
                new Student(10006, "Frank", new Course(5, "Biology", "Emma Wilson")),
                new Student(10007, "Grace", new Course(3, "History", "Susan Johnson")),
                new Student(10008, "Henry", new Course(6, "Chemistry", "William Garcia")),
                new Student(10009, "Ivy", new Course(7, "Music", "Olivia Davis")),
                new Student(10010, "Jack", new Course(8, "Art", "Michael Taylor"))
            };

查询名字以 "A" 开头的学生 

 var query1 = studentList.Where(s => s.Name.StartsWith("A"));

查询参加数学课程的学生 

var query2 = studentList.Where(s => s.Course.Name == "Mathematics");

查询参加数学课程的学生数量

var count = studentList.Count(s => s.Course.Name == "Mathematics");

按学生姓名进行升序排序

var query3 = studentList.OrderBy(s => s.Name);

查询参加不同课程的学生姓名列表

var query4 = studentList.Select(s => s.Name).Distinct();

查询参加数学课程的学生姓名列表

 var query5 = studentList
                .Where(s => s.Course.Name == "Mathematics")
                .Select(s => s.Name);

返回第一个参加历史课程的学生

var firstStudent = studentList.FirstOrDefault(s => s.Course.Name == "History");

相关推荐

  1. Linq练习

    2024-01-10 15:04:04       42 阅读
  2. LINQ【C#】

    2024-01-10 15:04:04       33 阅读
  3. LINQ简述

    2024-01-10 15:04:04       41 阅读
  4. C# <span style='color:red;'>LINQ</span>

    C# LINQ

    2024-01-10 15:04:04      30 阅读
  5. 练 习

    2024-01-10 15:04:04       43 阅读
  6. c# 学习笔记 - LINQ

    2024-01-10 15:04:04       33 阅读
  7. c# linq 查询

    2024-01-10 15:04:04       25 阅读
  8. C#编写LINQ查询

    2024-01-10 15:04:04       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-10 15:04:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-10 15:04:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-10 15:04:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-10 15:04:04       18 阅读

热门阅读

  1. Android 无限循环RecyclerView的完美实现方案

    2024-01-10 15:04:04       30 阅读
  2. Flink之Task重启策略

    2024-01-10 15:04:04       35 阅读
  3. 如何使用PHP开发缓存优化图片加载速度

    2024-01-10 15:04:04       30 阅读
  4. 广度优先搜索

    2024-01-10 15:04:04       33 阅读
  5. 理解DOM树的加载过程

    2024-01-10 15:04:04       34 阅读
  6. Centos 7 安装Node.js服务

    2024-01-10 15:04:04       40 阅读
  7. 机器人控制箱内部包含什么零件,有什么作用。

    2024-01-10 15:04:04       37 阅读
  8. 【Verilog】期末复习——设计11011序列检测器电路

    2024-01-10 15:04:04       35 阅读
  9. #Uniapp:编译器#ifdef --- #endif &#ifndef --- #endif

    2024-01-10 15:04:04       42 阅读