05、linq基本使用

  1. 什么是LINQ?LINQ即语言集成查询。它是.NET框架的一个扩展,允许我们以使用SQL语句类似的方式查询数据集合。linq作为
    IEnumerable接口的扩展方法故只要实现了该接口的方法都可以使用LINQ语法。
    首先展示一下where方法的使用。
    复制代码
     1  static void Main(string[] args)
     2         {
     3             //使用linq语法输出大于10的数字
     4             int[] nums = new int[] { 3, 5, 9, 12, 14, 15, 89, 6 };
     5             //x代表数组中的每一项。where方法会遍历集合中的每一个元素,对于每个元素都会调用a => a>10这个表达式判断一下是否为true。
     6             //如果为true,则把这个元素放在返回的集合中
     7             IEnumerable<int> result = nums.Where(x => x > 10);
     8             foreach (var item in result)
     9             {
    10                 Console.WriteLine(item);
    11             }
    12         }
    复制代码

    注:yeild return 与return的区别。

    复制代码
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 
     5 namespace LearningTest
     6 {
     7     using System;
     8     using System.Collections.Generic;
     9 
    10     namespace ConsoleApp
    11     {
    12         class ClassTest
    13         {
    14             static void Main(string[] args)
    15             {
    16                 Console.WriteLine("不使用 yield return 方式的结果");
    17                 foreach (var item in WithoutYield())
    18                 {
    19                     Console.WriteLine(item);
    20                 }
    21                 Console.WriteLine("使用 yield return 方式的结果");
    22                 foreach (var item in WithYield())
    23                 {
    24                     Console.WriteLine(item);
    25                 }
    26                 Console.ReadLine();
    27             }
    28             //不使用 yield return
    29             static IEnumerable<int> WithoutYield()
    30             {
    31                 List<int> result = new List<int>();
    32                 foreach (int i in GetData())
    33                 {
    34                     if (i > 2)
    35                     {
    36                         result.Add(i);
    37                     }
    38                 }
    39                 return result;
    40             }
    41             // 使用 yield return
    42             static IEnumerable<int> WithYield()
    43             {
    44                 foreach (int i in GetData())
    45                 {
    46                     if (i > 2)
    47                     {
    48                         yield return i;
    49                     }
    50                 }
    51                 yield break;
    52                 Console.WriteLine("这里的代码不执行");
    53             }
    54             //获取数据
    55             static List<int> GetData()
    56             {
    57                 return new List<int>() { 1, 2, 3, 4 };
    58             }
    59         }
    60     }
    61 }
    复制代码

    以上代码都输出了3,4.其区别在于

    第一种return方式,是把结果集全部加载到内存中再遍历;

    第二种yeild return方式,遍历每调用一次,yield return就返回一个值;

    因此,当希望获取一个IEnumerable<T>类型的集合,而不想把数据一次性加载到内存,就可以考虑使用yield return的方式去实现;

  2. LINQ常用的扩展方法之count方法:主要获取数据的总条数。
    1 static void Main(string[] args)
    2         {
    3             //使用linq语法输出大于10的数字的个数
    4             int[] nums = new int[] { 3, 5, 9, 12, 14, 15, 89, 6 };
    5             int count = nums.Count(x => x > 10);
    6             Console.WriteLine(count);//输出4
    7         }

    获取大于10或者小于5的数据总条数。

    1 static void Main(string[] args)
    2         {
    3             //使用linq语法输出大于10的数字的个数
    4             int[] nums = new int[] { 3, 5, 9, 12, 14, 15, 89, 6 };
    5             int count = nums.Count(x => x > 10 || x < 5);
    6             Console.WriteLine(count);//输出5
    7         }

     

  3. LINQ常用方法之Any方法,指的是数据中是否有一条数据符合要求。

    1 static void Main(string[] args)
    2         {
    3             //使用linq语法输出大于10的数字的个数
    4             int[] nums = new int[] { 3, 5, 9, 12, 14, 15, 89, 6 };
    5             bool result = nums.Any(x => x > 100);
    6             Console.WriteLine(result);//false
    7         }
  4. Single方法:有且只有一条满足要求的数据;如果数据中满足条件的数据有多个或者不存在满足条件的数据,该方法都会报错。
  5. SingleOrDefault方法:最多只有一条满足要求的数据;如果数据中满足条件的数据有多个,则该方法会报错。如果不存在满足条件的数据,对于引用类型返回null,对于值类型,则返回默认值。比如整数类型,默认值就是0。
  6. First方法:至少有一条数据,返回第一条数据。如果满足条件的数据一条都没有,则会报错。
  7. FirstOrDefault方法:返回第一条或者默认值;如果满足条件的数据一条都没有,则会返回默认值。
  8. Order排序:对数据进行正序排序。原来数据的顺序不受影响,返回的是新的结果。order函数后面还可以使用thenby()函数,可以实现在第一个排序标准相同的情况下,按照第二种排序规则进行排序。
  9. OrderByDescending():倒序排序。order函数后面还可以使用thenbyDescending()函数,可以实现在第一个排序标准相同的情况下,按照第二种排序规则进行排序。
  10. Skip(n)跳过n条数据,Take(n)获取n条数据。Skip函数和Take函数可以单独使用。
posted @   电竞~马保国  阅读(106)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示