这个错误,在使用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. 如何通过Restful API的方式读取SAP Commerce Cloud的Product Reference

    从SAP官网上找到api的说明: https://api.sap.com/api/commerce_services/resource api endpoint: /rest/v2/electroni ...

  2. 在SAP Hybris commerce Storefront里购物下单

    操作过程和大家平时在网上购物没有太大差别. 选中一款心仪的产品,点击Add to cart加到购物车里: 点击Check out结帐: 生成一个购物车ID: 维护发货地址: 维护发货方式: 点击Pla ...

  3. cal of easter egg

    在Linux中用cal命令查看1752年9月,发现3到13日全都消失了……

  4. SpringCloud学习心得之Eureka注册中心的基本使用

      SpringCloud学习心得——Eureka注册中心 示范代码链接 定义 SpringCloud Eureka是 SpringCloud Netflix微服务套件的一部分,基于 REST 的服务 ...

  5. java基础(13)---集合框架

    一.集合框架 Java的集合类是一些非常实用的工具类,主要用于存储和装载数据 (包括对象),因此,Java的集合类也被成为容器.在Java中,所有的集合类都位于java.util包下,这些集合类主要是 ...

  6. vm.$attrs与inheritAttrs详解

    1. inheritAttrs 在vue官网的解释如下 个人理解:父组件A上引入子组件B,在B子组件上加上一些属性(class.style除外),这些属性能否在子组件B的根元素上继承,默认值为true ...

  7. 浏览器缓存控制 以及 在url框中回车、F5 和 Ctrl + F5的区别

    第一部分: 浏览器缓存如何控制?   做网站,不知道缓存是什么东西怎么能行! 如何实现HTTP缓存呢? 下面我们来一步一步的探寻实现机制把. 方案一: 无缓存   说明:浏览器向服务器请求资源m.pn ...

  8. 1.什么是spring

    spring框架是一个为java应用程序的开发提供了综合,广泛的基础性支持的java开源框架, 使得开发者不用去关心一些冗杂的基础性问题,从而可以更加专注于应用程序的开发. spring框架是一个分层 ...

  9. 题解 UVa10892

    题目大意 多组数据,每组数据给定一个整数 \(n\),求满足 \(LCM(x,y)=n\) 的不同无序整数对 \((x,y)\) 的数目. 分析 若有 \(LCM(x,y)=n\),则有 \(GCD( ...

  10. 有关 Java (jackson包问题 ,MappingJacksonHttpMessageConverter 和 MappingJackson2HttpMessageConverter问题)

    今天这一系列问题吃掉我四个小时,所以现在吸收掉. 一. 整理所有错误信息: 1.错误信息:java.lang.NoClassDefFoundError: Could not initialize cl ...