在一个linq查询中使用了平均值聚合函数Average,结果报错 System.InvalidOperationException Sequence contains no elements (序列不包含任何元素) 也就是说,使用某些linq的函数时,如果值不存在是会报错的,比如: First()Single()FirstAsync()SingleAsync()Last()LastAsync()Max()Min()Average() 等, 解决方案: 使用空...
When you start playing with LINQ queries over sequences of elements (e.g. getting min / max value for enumerable source) sooner or later you will come across this one -- the InvalidOperationException (“Sequence contains no elements”). The problem occurs as by default queries like IEnumerabl...
Returns the first element of the sequence that satisfies a condition or a default value if no such element is found. FirstOrDefault<TSource>(IEnumerable<TSource>) Returns the first element of a sequence, or a default value if the sequence contains no elements. ...
Returns the first element of the sequence that satisfies a condition or a default value if no such element is found. FirstOrDefault<TSource>(IEnumerable<TSource>) Returns the first element of a sequence, or a default value if the sequence contains no elements. GroupBy<TSource,TKey>(IEnumer...
Returns the first element of the sequence that satisfies a condition or a default value if no such element is found. FirstOrDefault<TSource>(IEnumerable<TSource>) Returns the first element of a sequence, or a default value if the sequence contains no elements. ...
A sequence of Single values to determine the maximum value of. Returns Single The maximum value in the sequence. Exceptions ArgumentNullException source is null. InvalidOperationException source contains no elements. Remarks The Max(IEnumerable<Single>) method uses the Single implementation of ICo...
If there are no items, First and Single will throw an InvalidOperationException with the message “Sequence contains no elements.” FirstOrDefault and SingleOrDefault protect you from the exception by returning the default, which is generally a null (Nothing in VB). Additionally, if you use ...
InvalidOperationException source contains no elements. Remarks The Max<TSource>(IEnumerable<TSource>, Func<TSource, Decimal>) method uses the Decimal implementation of IComparable<T> to compare values. You can apply this method to a sequence of arbitrary values if you provide a functio...
Returns a sequence that only contains the elements from the source that satisfy the predicate. This function is sometimes also called filter (e.g. in Java Streams, JavaScript) and can also be used as such.local seq = linq { { name = "Jane Doe", role = "Manager" }, { name = "...
true if the two source sequences are of equal length and their corresponding elements are equal according to the default equality comparer for their type; otherwise, false. Usage Note In Visual Basic and C#, you can call this method as an instance method on any object of type I...