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 ...
随机推荐
- django同一个项目中连接多个数据库
一.场景与思路 同一个项目中需要连接多个数据库. 二.代码 代码中主要是三个部分,settings.models以及自己写的一个类. 1.自己写的文件:database_app_router.py ...
- RabbitMQ topic 交换器
topic交换器:"."将路由键分为几个标识符,"*"匹配一个, "#"可以匹配多个 1:路由键为*或者#的时候 *:只能匹配单个的字符串 ...
- mysql 创建新用户 并赋予权限
1.以管理员身份登录mysql mysql -u root -p 2.选择mysql数据库 use mysql 3.创建用户并设定密码 create user 'testuser'@'localhos ...
- Something is already running on port 3000. Would you like to run the app on another port instead?
查看端口sudo lsof -i :3000 删除进程 sudo kill -9 12297[pid]
- P1072 Hankson 的趣味题[数论]
题目描述 Hanks 博士是 BT(Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲解了 ...
- H5性能测试,首屏时间统计(Argus)
Argus 腾讯质量开发平台,官网链接:https://wetest.qq.com/product/argus 主要针对性:H5的游戏性能测试 主要介绍: 独家首屏时间统计: 告别人工掐秒 自动统计首 ...
- Java方法注意事项
使用方法的注意事项: 1.方法应该定义在类中,方法中不能再定义方法,也就是不能嵌套定义,但方法可以中可以调用方法 2.方法定义的前后顺序无所谓,执行的先后顺序只与调用有关 3.方法定义之后不会执行,如 ...
- 安装 selenium 对于python而言属于一个第三方的模块
针对第三方的模块,如何安装 在dos界面输入python -m pip install 模块名称 安装相关的浏览器以及浏览器的驱动 下载谷歌浏览器的驱动,淘宝镜像 下载后,解压,然后将得到的exe文件 ...
- python链接oracle数据库以及数据库的增删改查实例
初次使用python链接oracle,所以想记录下我遇到的问题,便于向我这样初次尝试的朋友能够快速的配置好环境进入开发环节. 1.首先,python链接oracle数据库需要配置好环境. 我的相关环境 ...
- c# 自动将string字符串转成实体属性的类型
Convert.ChangeType() 看到.net webapi中有[FromUri]来接收参数 可以将自动参数转换成字段属性的类型 baidu 了许多文章 都在自己造轮子 突然发下微软提供了 ...