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 ...
随机推荐
- java23种设计模式专攻:生产者-消费者模式的三种实现方式
公司的架构用到了dubbo.带我那小哥也是个半吊子,顺便就考我生产者消费者模式,顺便还考我23种java设计模式,
- obj + mtl 格式说明
OBJ(或 .OBJ)是一种开放的几何定义文件格式,最初由Wavefront Technologies公司开发,用以描述其Advanced Visualizer动画包.该格式已被其他3D图形应用供应商 ...
- prometheus 告警规则
GitHub网址1 https://github.com/samber/awesome-prometheus-alerts 网址2 https://awesome-prometheus-alerts. ...
- QtCreator常用快捷键
1)帮助文件:F1 (光标在函数名字或类名上,按 F1 即可跳转到对应帮助文档,查看其详细用法) 2).h 文件和对应.cpp 文件切换:F4 3)编译并运行:Ctrl + R 4)函数声明和定义(函 ...
- 8.效果控件之移动&&缩放
1.移动应用(横摇.竖摇) 1.移动应用(横向缩小跟踪排列) 1.移动应用(自由缩小跟踪排列)
- Fiddler使用资料-整理
以下是一个博主写的一个系列. 随笔分类 - Fiddler 10.Fiddler中设置断点修改Response 摘要:当然Fiddler中也能修改Response 第一种:打开Fiddler 点击 ...
- Luogu P1627 中位数
Luogu P1627 中位数 先记录目标数的位置,并且把数组映射为: $$a[i]=\begin{cases}-1,a[i]<b\0,a[i]=b\1,a[i]>b\end{cases} ...
- docer安装之pure-ftp
https://hub.docker.com/r/stilliard/pure-ftpd Docker Pure-ftpd Server https://hub.docker.com/r/stilli ...
- Spark-2.3.2 HBase BulkLoad
在大量数据需要写入HBase时,通常有Put方式和BulkLoad两种方式. Put不做解释. BulkLoader方式的优势在于: 1.不会触发WAL预写日志,当表还没有数据时进行数据导入不会产生F ...
- 【二分答案】Expanding Rods POJ 1905
题目链接:http://poj.org/problem?id=1905 题目大意:原长度为L的线段因受热膨胀为一段弧,线段L.弧长L'.温度n.膨胀率c满足L' =(1+n/c)*L;求线段的中点移动 ...