C#中Linq延迟查询的例子
作者:junjie 时间:2022-01-15 02:43:11
提出问题
下面所给代码编译时正常,但是执行时会出错,请指出程序在执行时能够执行到编号为(1)(2)(3)的代码行中的哪一行。
using System;
using System.Collections.Generic;
using System.Linq;
namespace DeferredExecutionExp
{
class Program
{
static void Main(string[] args)
{
List<Student> studentList = new List<Student>()
{
new Student(){Id =1, Name="ZhangSan", Age=20},
new Student(){Id =2, Name=null, Age=21},
new Student(){Id =3, Name="Lisi", Age=22}
};
var queryedStudentList = studentList.Where(it => it.Name.Trim() != "ZhangSan");//(1)
if (queryedStudentList.Count() > 0)//(2)
{
foreach (var student in queryedStudentList)//(3)
{
Console.WriteLine(student.Name);
}
}
}
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
问题分析
其实,发现问题并不难,很显然在执行代码“it => it.Name.Trim()”时会出错,因为集合studentList中第二个学生的Name属性为null,当遍历到该学生时,对其Name属性执行Trim操作,不出错才怪呢。既然在这里会出错,那么程序肯定是执行到该行就GameOver了呀。但是实际情况又会是这样吗?
动手验证
单步调试程序,发现:执行到代码行(1)时,程序并没有出错,而是在执行代码行(2)时,程序才出现了异常,查看异常信息,却提示的是在执行代码行(1)时有问题,为什么会这样呢?为什么执行到代码行(2)时还会去执行代码行(1)呢?这全都是因为Linq的延迟查询导致的。
延迟查询
延迟查询的意思是说:在运行期间定义查询表达式时,查询不会执行,只有到迭代数据项时查询才会被执行。本例中的代码行(1)只是定义了查询,而代码行(2)中当调用Count方法时便会遍历数据项,这时便会执行查询,也就是说会去执行代码行(1)定义的查询,最终导致了本例中这种现象的出现。
所以,本例中的代码最终能够执行到的代码行是(2)。
标签:C#,Linq,延迟查询
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java线程池的优点及池化技术的应用
2022-07-01 08:12:23
![](https://img.aspxhome.com/file/2023/0/72290_0s.webp)
Flutter 底部弹窗如何实现多项选择
2023-06-24 17:08:17
![](https://img.aspxhome.com/file/2023/7/84707_0s.gif)
详解MyBatis的Dao层实现和配置文件深入
2022-07-26 02:29:17
![](https://img.aspxhome.com/file/2023/6/61246_0s.jpg)
java中this的n种使用方法
2023-12-23 13:13:23
![](https://img.aspxhome.com/file/2023/3/62063_0s.png)
Java 替换word文档文字并指定位置插入图片
2023-08-12 22:02:25
![](https://img.aspxhome.com/file/2023/8/64368_0s.png)
Java SpringCache+Redis缓存数据详解
2023-11-29 01:01:05
![](https://img.aspxhome.com/file/2023/1/60131_0s.jpg)
Java多线程的其他知识_动力节点Java学院整理
2023-09-06 06:01:58
![](https://img.aspxhome.com/file/2023/2/68032_0s.png)
MybatisPlus多表连接查询的问题及解决方案
2023-11-25 22:09:06
![](https://img.aspxhome.com/file/2023/8/60348_0s.png)
Linux下Hadoop 2.7.3 安装搭建过程
2023-11-08 05:13:59
![](https://img.aspxhome.com/file/2023/0/58870_0s.png)
详解java Collections.sort的两种用法
2023-11-28 09:30:31
![](https://img.aspxhome.com/file/2023/3/60473_0s.png)
使用springboot整合RateLimiter限流过程
2022-09-12 21:42:48
![](https://img.aspxhome.com/file/2023/5/61655_0s.png)
SSM项目使用拦截器实现登录验证功能
2023-06-17 16:12:38
![](https://img.aspxhome.com/file/2023/0/65600_0s.png)
Java Socket实现单线程通信的方法示例
2022-04-22 15:43:02
java:无法访问org.springframework.boot.SpringApplication的解决方法
2022-07-14 05:24:29
![](https://img.aspxhome.com/file/2023/1/67701_0s.jpg)
Maven分模块开发执行指令失败的问题
2021-07-10 19:00:15
![](https://img.aspxhome.com/file/2023/0/72560_0s.jpg)
Android使用Websocket实现聊天室
2023-07-07 13:01:59
![](https://img.aspxhome.com/file/2023/6/60386_0s.png)
Java异步调用转同步方法实例详解
2023-11-01 20:38:25
Spring Security实现用户名密码登录详解
2021-05-24 14:32:36
![](https://img.aspxhome.com/file/2023/1/61051_0s.png)
使用SpringBoot获取resources文件路径
2022-10-12 15:29:56
![](https://img.aspxhome.com/file/2023/9/66639_0s.png)
SpringMVC中Controller类数据响应的方法
2021-10-07 21:00:08
![](https://img.aspxhome.com/file/2023/0/75710_0s.png)