Sequence contains no elements
这个错误,在使用List<T>的First函数遇到。
Sequence contains no elements?
From "Fixing LINQ Error: Sequence contains no elements":
When you get the LINQ error "Sequence contains no elements", this is usually because you are using the
First()orSingle()command rather thanFirstOrDefault()andSingleOrDefault().
This can also be caused by the following commands:
FirstAsync()SingleAsync()Last()LastAsync()Max()Min()Average()Aggregate()
When to use .First and when to use .FirstOrDefault with LINQ?
I would use First() when I know or expect the sequence to have at least one element. In other words, when it is an exceptional occurrence that the sequence is empty.
Use FirstOrDefault() when you know that you will need to check whether there was an element or not. In other words, when it is legal for the sequence to be empty. You should not rely on exception handling for the check. (It is bad practice and might hurt performance).
Finally, the difference between First() and Take(1) is that First() returns the element itself, while Take(1) returns a sequence of elements that contains exactly one element.
.First() will throw an exception if there's no row to be returned, while .FirstOrDefault() will return the default value (NULL for all reference types) instead.
So if you're prepared and willing to handle a possible exception, .First() is fine. If you prefer to check the return value for != null anyway, then .FirstOrDefault() is your better choice.
But I guess it's a bit of a personal preference, too. Use whichever makes more sense to you and fits your coding style better.
Sequence contains no elements的更多相关文章
- EF Core Sequence contains no elements
一.在.Net Core中使用Ef的聚合函数报错: 类似函数: 1,使用FirstOrDefault() 来代替 First() 2.使用SingleOrDefault 来代替 Single 3.使用 ...
- System.InvalidOperationException: Sequence contains no elements
foreach (var keyCode in unexpectedKeyCodesDetected) { string unexpected = expectedCapturedKeyCodes.W ...
- Sequence contains no elements : LINQ error
1.错误意思: 出现错误的原因是:你要从一个null中取的数据. 2.错误的处理 1,使用FirstOrDefault() 来代替 First() 2.使用SingleOrDefault 来代替 Si ...
- Codeforces 327B-Hungry Sequence(素数筛)
B. Hungry Sequence time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- James Munkres Topology: Lemma 21.2 The sequence lemma
Lemma 21.2 (The sequence lemma) Let \(X\) be a topological space; let \(A \subset X\). If there is a ...
- codeforces 894C - Marco and GCD Sequence - [有关gcd数学题]
题目链接:https://cn.vjudge.net/problem/CodeForces-894C In a dream Marco met an elderly man with a pair o ...
- CF 327B. Hungry Sequence
B. Hungry Sequence time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces 894.C Marco and GCD Sequence
C. Marco and GCD Sequence time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces Round #191 (Div. 2) B. Hungry Sequence(素数筛选法)
. Hungry Sequence time limit per test 1 second memory limit per test 256 megabytes input standard in ...
随机推荐
- Computer Vision_33_SIFT: A novel point-matching algorithm based on fast sample consensus for image registration——2015
此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...
- 阿里云SOP
阿里云SOP 摘要 注册阿里云账号. 领取及配置ECS. 领取及配置RDS. 部署网站. 注册阿里云账号 在主页点击注册 填入相应的信息 领取及配置ECS 注册后领取免费的ECS,RDS. 打开控制台 ...
- UVA572 (并查集解法)
紫书第六章的题,用并查集重写一遍,果然还是书上给的dfs方法更简单...... 一开始用fa[i*m+j] != i*m+j 来判断是否访问过,虽然没想出来为什么WA,但是确实不如用vis稳 #inc ...
- 【转】IP报文格式详解
下图为常见的IP报文格式表: 上面是IP的报文格式,接下来我们先说明各个字段的意义.然后,用Etheral软件转包分析IP的报文格式. 1.版本:ip报文中,版本占了4位,用来表示该协议采用的是那一个 ...
- Spring boot集成Websocket,前端监听心跳实现
第一:引入jar 由于项目是springboot的项目所以我这边简单的应用了springboot自带的socket jar <dependency> <groupId>org. ...
- 登录-redis
session的问题 目前session直接是js变量,放在nodejs进程内存中 1.进程内存有限,访问量过大,内存暴增怎么办? 2.正式线上运行是多进程,进程之间内存无法共享 为何session适 ...
- Luogu P1339 热浪Heat Wave
Luogu P1339 热浪Heat Wave 裸·单源最短路. 但是! 有以下坑点: 算过复杂度发现Floyd跑不过去就不要用了. 如果建边是双向边,边的数组大小要开两倍! 考场上如果再把初始化的$ ...
- [software test - 001] Why we need software test?
/* This is a conclusion about the software testing job. */ /* Scope: middle level software tasks, ...
- 收起.NET程序的dll来
作为上床后需要下床检查好几次门关了没有的资深强迫症患者,有一个及其搞我的问题,就是dll问题. 曾几何时,在没有nuget的年代,当有依赖项需要引用的时候,只能通过文件引用来管理引用问题,版本问题,更 ...
- LG4721 【模板】分治 FFT
P4721 [模板]分治 FFT 题目背景 也可用多项式求逆解决. 题目描述 给定长度为 $n-1$ 的数组 $g[1],g[2],..,g[n-1]$,求 $f[0],f[1],..,f[n-1]$ ...