这个错误,在使用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. 阅读笔记---第三章 Xen信息页

    1.文件/xen/include/public/xen.h 2.第一个数据结构:启动信息页strat_info,启动信息页是GuestOS内核启动时,由Xen映射到GusetOS内存空间的一个物理页面 ...

  2. 4.kafka API producer

    1.Producer流程首先构建待发送的消息对象ProducerRecord,然后调用KafkaProducer.send方法进行发送.KafkaProducer接收到消息后首先对其进行序列化,然后结 ...

  3. SQL SERVER-查询存储运行状态

    SELECT p.name as SPName, qs.last_elapsed_time as [LastExecTime(ms)], (total_elapsed_time as [AvgExec ...

  4. Android笔记(四十三) Android中的数据存储——SQLite(五)delete

    SQLite通过delete()方法删除数据 delete()方法参数说明: delete()方法参数 对应sql部分 描述 table delte from table_name 要删除的表 whe ...

  5. django后台xadmin如下配置(小结)

    django-admin文档:https://xadmin.readthedocs.io/en/latest/index.html目录: 1.xadmin基本配置 2.配置后台显示的模型类 3.后台注 ...

  6. keepalived,tomcat,memcache

    1.Nginx+Keepalived实现站点高可用 linux cluster类型 LB:nginx负载,varnish(director module)haproxy,lvs HA:keepaliv ...

  7. Kubernetes-Istio之Sidecar自动注入

    前提: (官方提供) 1):确认使用的是Kubernetes服务器的受支持版本( 1.13.1.14.1.15):kubectl (官方提供,应该是1.13版本以上,我的是1.16版本) kubect ...

  8. CentOS7编译安装httpd-2.4.41 php7.3

    CentOS7编译安装httpd-2.4.41 php7.3 安装参考环境: CentOS Linux release 7.5.1804 (Core) 一.安装依赖包 httpd安装的依赖包 # yu ...

  9. Codeforces D. Color the Fence(贪心)

    题目描述: D. Color the Fence time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  10. 《代码敲不队》第九次团队作业:Beta冲刺第3天

    项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 代码敲不队 作业学习目标 (1)项目文档的完善与整理:(2)团队项目总结陈述PPT编制:(3)符合 ...