• C#中的LINQ(Language-Integrated Query)


    C#中的LINQ(Language-Integrated Query)是一种强大的查询语言和查询操作符集合,用于对各种数据源(如集合、数组、数据库等)进行查询、筛选、排序和转换操作。LINQ提供了一种统一的语法和编程模型,使得数据查询和处理变得更加简洁、可读性更高。

    下面是LINQ的几种常见用法及其详细解释和示例:

    1. 查询语法(Query Syntax):
      查询语法使用类似于SQL的语法,通过关键字(如fromwhereselect等)来描述查询操作。它能够对数据源进行筛选、投影、排序等操作。

      示例:

      int[] numbers = { 1, 2, 3, 4, 5, 6 };
      var evenNumbers = from num in numbers
                        where num % 2 == 0
                        select num;
      foreach (var num in evenNumbers)
      {
          Console.WriteLine(num);
      }
      // 输出:2 4 6
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    2. 方法语法(Method Syntax):
      方法语法使用扩展方法和操作符来对数据源进行查询操作。它以方法链的形式进行调用,每个方法代表一个查询操作。

      示例:

      int[] numbers = { 1, 2, 3, 4, 5, 6 };
      var evenNumbers = numbers.Where(num => num % 2 == 0);
      foreach (var num in evenNumbers)
      {
          Console.WriteLine(num);
      }
      // 输出:2 4 6
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    3. Lambda 表达式:
      Lambda 表达式用于定义匿名函数,常用于 LINQ 查询中的条件筛选和投影操作。

      示例:

      int[] numbers = { 1, 2, 3, 4, 5, 6 };
      var evenNumbers = numbers.Where(num => num % 2 == 0);
      foreach (var num in evenNumbers)
      {
          Console.WriteLine(num);
      }
      // 输出:2 4 6
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    4. 延迟执行(Deferred Execution):
      LINQ 查询具有延迟执行的特性,即查询不会立即执行,而是在结果被枚举或显式调用执行操作时才执行。

      示例:

      int[] numbers = { 1, 2, 3, 4, 5, 6 };
      var query = numbers.Where(num => num % 2 == 0);
      numbers[0] = 10;
      foreach (var num in query)
      {
          Console.WriteLine(num);
      }
      // 输出:10 2 4 6
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
    5. 连接查询(Join Query):
      连接查询用于将多个数据源进行关联,并对关联结果进行查询操作。

      示例:

      var students = new List<Student>
      {
          new Student { Id = 1, Name = "Alice", Age = 20 },
          new Student { Id = 2, Name = "Bob", Age = 22 },
          new Student { Id = 3, Name = "Charlie", Age = 21 }
      };
      
      var scores = new List<Score>
      {
          new Score { StudentId = 1, Subject = "Math", Grade = 80 },
          new Score { StudentId = 2, Subject = "Math", Grade = 90 },
          new Score { StudentId = 3, Subject = "Math", Grade = 85 }
      };
      
      var query = from student in students
                  join score in scores on student.Id equals score.StudentId
                  where score.Grade >= 85
                  select new { student.Name, score.Subject };
      
      foreach (var result in query)
      {
          Console.WriteLine($"{result.Name} - {result.Subject}");
      }
      // 输出:Alice - Math
      //       Bob - Math
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
    6. 分组查询(Grouping Query):
      分组查询用于将数据源按照指定的键进行分组,并对每个组进行聚合或查询操作。

      示例:

      var students = new List<Student>
      {
          new Student { Id = 1, Name = "Alice", Age = 20 },
         I apologize for the incomplete response. Here are a few more examples of LINQ usage in C#:
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
    7. 聚合函数(Aggregate Functions):
      聚合函数用于对数据源进行聚合操作,如计算总和、平均值、最大值、最小值等。

      示例:

      int[] numbers = { 1, 2, 3, 4, 5 };
      int sum = numbers.Sum();
      int max = numbers.Max();
      int min = numbers.Min();
      double average = numbers.Average();
      
      Console.WriteLine($"Sum: {sum}");             // 输出:Sum: 15
      Console.WriteLine($"Max: {max}");             // 输出:Max: 5
      Console.WriteLine($"Min: {min}");             // 输出:Min: 1
      Console.WriteLine($"Average: {average}");     // 输出:Average: 3
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    8. 排序(Sorting):
      使用 OrderByOrderByDescending 方法对数据进行排序。

      示例:

      int[] numbers = { 5, 2, 1, 4, 3 };
      var sortedNumbers = numbers.OrderBy(num => num);
      
      foreach (var num in sortedNumbers)
      {
          Console.WriteLine(num);
      }
      // 输出:1 2 3 4 5
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
    9. 转换(Projection):
      使用 Select 方法对数据进行转换,可以选择要返回的字段或进行复杂的转换操作。

      示例:

      var students = new List<Student>
      {
          new Student { Id = 1, Name = "Alice", Age = 20 },
          new Student { Id = 2, Name = "Bob", Age = 22 },
          new Student { Id = 3, Name = "Charlie", Age = 21 }
      };
      
      var studentNames = students.Select(student => student.Name);
      
      foreach (var name in studentNames)
      {
          Console.WriteLine(name);
      }
      // 输出:Alice Bob Charlie
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14

    这些示例涵盖了LINQ的一些常见用法,包括查询语法、方法语法、Lambda表达式、延迟执行、连接查询、分组查询、聚合函数、排序和转换。通过利用LINQ的强大功能,可以简化和提高数据处理和查询的效率。

  • 相关阅读:
    电脑监控软件是如何提高员工工作效率的?
    Android学习笔记 54. Gradle入门
    C++中istream_iterator和ostream_iterator的源码分析
    他没有输出一直卡在后面不懂,看报错应该是卡在# 循环生成诗句了
    千万别再一个人闷头做事了
    【如何学习Python自动化测试】—— 自动化测试环境搭建
    如何用ATECLOUD芯片测试系统测试电源芯片的振荡频率?
    airsim中连接vr的视角问题
    华为云云耀云服务器L实例评测 | 强大性能与高可靠性的完美结合
    中国智能音箱行业前景预测与市场调查研究报告
  • 原文地址:https://blog.csdn.net/ultramand/article/details/138202396