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 ...
随机推荐
- 【填坑】Ubuntu安装vsftpd
1.安装vsftpdsudo apt-get install vsftpd 安装完毕后或许会自动生成一个帐户"ftp",/home下也会增加一个文件夹.如果没有生成这个用户的话可以 ...
- centos7 安装 git服务器
服务器端配置 yum install -y git groupadd git useradd git -g git 2.创建authorized_keys cd /home/git mkdir .ss ...
- mac 下enable mysql的load data in file
1)使用root用户登录mysql 2)将 local_infile 变量设置为true SET GLOBAL local_infile = true; 3)重启数据库 在系统偏好设置中找到MySql ...
- 本地安装部署Jira
https://blog.csdn.net/u013492736/article/details/83315650 1. 首先在官网下自行搭建服务器的版本,有适合于windows的,也有linux版本 ...
- 深度学习 吴恩达深度学习课程2第三周 tensorflow实践 参数初始化的影响
博主 撸的 该节 代码 地址 :https://github.com/LemonTree1994/machine-learning/blob/master/%E5%90%B4%E6%81%A9%E8 ...
- springboot学习笔记(二)—— springboot的启动模式设置
把springboot的启动类图标(spring)去掉,在启动类中添加以下代码 package com.xdr.spring; import org.springframework.boot.Bann ...
- 下载安装Zookeeper
下载地址 http://archive.apache.org/dist/zookeeper/ 进入如上的Url,选择合适的zookeeper版本,下载"tar.gz"文件: 解压安 ...
- P2015 二叉苹果树[树形dp+背包]
题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来 ...
- 零基础如何学好python之变量
想要自学python,变量(variable)是必经之路,它是学习python初始时,就会接触到的一个新的知识点,也是一个需要熟知的概念.python是一种动态类型语言,在赋值的执行中可以绑定不同类型 ...
- 【Beta】 Scrum meeting 3
第三天 日期:2019/6/26 前言 组内进行第三次讨论,所有组员都到场,项目已经全部完成. 一.今天任务完成情况.成员贡献时间 组员 任务 贡献时间 徐浩杰 完成项目的全部测试,项目运行稳定 2h ...