这个错误,在使用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() or Single() command rather than FirstOrDefault() and SingleOrDefault().

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的更多相关文章

  1. EF Core Sequence contains no elements

    一.在.Net Core中使用Ef的聚合函数报错: 类似函数: 1,使用FirstOrDefault() 来代替 First() 2.使用SingleOrDefault 来代替 Single 3.使用 ...

  2. System.InvalidOperationException: Sequence contains no elements

    foreach (var keyCode in unexpectedKeyCodesDetected) { string unexpected = expectedCapturedKeyCodes.W ...

  3. Sequence contains no elements : LINQ error

    1.错误意思: 出现错误的原因是:你要从一个null中取的数据. 2.错误的处理 1,使用FirstOrDefault() 来代替 First() 2.使用SingleOrDefault 来代替 Si ...

  4. Codeforces 327B-Hungry Sequence(素数筛)

    B. Hungry Sequence time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. 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 ...

  6. 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 ...

  7. CF 327B. Hungry Sequence

    B. Hungry Sequence time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. js javascirpt 数学库、 算法库 (转载)

    提示:国外官网,谷歌浏览器右键可以翻译成中文. 1.math.js 官网:https://mathjs.org/index.html 其它简介:https://www.jianshu.com/p/4f ...

  2. MySQL DataType--隐式类型转换

    隐式类型转换 在官方文档中对隐式类型转换规则有如下描述: 1. If one or both arguments are NULL, the result of the comparison is N ...

  3. docker部署beego环境解决golang三方依赖库问题

    直接上图,有图有真相 Dockerfile文件中 运行dockercompose命令即可 以上请注意路项目路徑不要搞错

  4. Debian9.5系统安装

    1.镜像下载地址 http://cdimage.debian.org/cdimage/archive/ 2.开始安装 如果有配置网络地址,可以手动配置或者跳过等系统安装好后配置.  至此debian9 ...

  5. Hadoop 二次排序

    需求 求每年的最高气温,年份升序,温度求最高 数据源内容如下 temperature.txt 2004 49 1981 -22 1981 -31 1965 -47 2027 -2 1964 6 203 ...

  6. Yarn Nodemanager启动不了报YarnRuntimeException: Failed to initialize container executor error=13 权限不够

    1.现象:有一个节点的NodeManager启动不了. 后台报错日志如下: org.apache.hadoop.yarn.exceptions.YarnRuntimeException: Failed ...

  7. Access、Trunk和Hybrid三种端口模式

    网络交换机(英语:Network switch)是一个扩大网络的器材,能为子网中提供更多的连接端口,以便连接更多的电脑. 通俗来说其起到的作用就是把一个网络端口分成多个网络端口 交换机和路由器的区别 ...

  8. springboot学习笔记(二)—— springboot的启动模式设置

    把springboot的启动类图标(spring)去掉,在启动类中添加以下代码 package com.xdr.spring; import org.springframework.boot.Bann ...

  9. linux一些配置

    ifconfig 查询.设置网卡和ip参数 ifup ens33 启动网卡 ifdown 关闭网卡 systemctl restart/start/stop network 重启.开始.关闭 网络服务 ...

  10. vue-cli 3 按需引入 element-ui

    1.安装按需引入必要插件 npm install babel-plugin-component --save-dev 2.修改babel.config.js 3.在main.js中引入用到的组件,例如 ...