System.InvalidOperationException: ‘sequence contains no elements’

huangapple go评论116阅读模式
英文:

System.InvalidOperationException: 'sequence contains no elements'

问题

我正试图使用用户名和密码登录,但它给了我上面的错误。

System.InvalidOperationException: ‘sequence contains no elements’

输入图像描述

英文:

I am trying to login with the help of username and password but it is giving me above error

System.InvalidOperationException: ‘sequence contains no elements’

enter image description here

答案1

得分: 1

Single()从集合中返回唯一的元素,或者返回满足指定条件的唯一元素。如果给定的集合中没有元素或多于一个元素,则Single()会引发InvalidOperationException异常。

SingleOrDefaultSingle()类似,只是如果没有元素与指定条件匹配,则返回通用集合底层类型的默认值。如果没有找到元素,它不会引发InvalidOperationException异常。但如果找到多个符合条件的元素,则会引发异常。

因此,根据您的情况,您可以选择使用SingleOrDefaultFirstOrDefault方法。

英文:

> Single() returns the only element from a collection, or the only
> element that satisfies the specified condition. If a given collection
> includes no elements or more than one elements then Single() throws
> InvalidOperationException.
>
> SingleOrDefault is just like Single() except that, if no element match the specified condition than it returns default value of underlying type of generic collection. It does not throws InvalidOperationException if no element found. But more than one element found for specified conditions than throws an exception..

So here you can choose to use SingleOrDefault or FirstOrDefault method according to your situation.

huangapple
  • 本文由 发表于 2023年4月17日 13:03:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定