在一个linq查询中使用了平均值聚合函数Average,结果报错 System.InvalidOperationException Sequence contains no elements (序列不包含任何元素) 也就是说,使用某些linq的函数时,如果值不存在是会报错的,比如: First()Single()FirstAsync()SingleAsync()Last()LastAsync()Max()Min()Average() 等, 解决方案: 使用空...
Linq error: Sequence contains no elements 程序报异常:Sequence contains no elements。首先想到 Oracle 数据库中用到的 Sequence 对象,查之未超限,.NextVal、.CurrVal 都正常。Google 之,异常是 IQuerable<T>.First() 方法报的,当查询的结果集为空时,调用 First() 方法将报此异常,换用 FirstOrDefault() 方法...
Today I got an exception saying "Sequence contains no elements " when using LINQ against Windows Azure tables. I did a quick search and found that it is due to that I used First<> instead of FirstOrDefault<> in my code. The below link explains the issue....
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...
A sequence of Single values to calculate the average of. Returns Single The average of the sequence of values. Exceptions ArgumentNullException source is null. InvalidOperationException source contains no elements. Remarks In Visual Basic query expression syntax, an Aggregate Into Average() clause...
Computes the average of a sequence ofSinglevalues that is obtained by invoking a projection function on each element of the input sequence. Cast<TResult>(IQueryable) Converts the elements of anIQueryableto the specified type. Chunk<TSource>(IQueryable<TSource>, Int32) ...
Determines whether a sequence contains any elements. Append<TSource>(IEnumerable<TSource>, TSource) Appends a value to the end of the sequence. AsEnumerable<TSource>(IEnumerable<TSource>) Returns the input typed asIEnumerable<T>. Average<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>) ...
Determines whether a sequence contains any elements. Append<TSource>(IEnumerable<TSource>, TSource) Appends a value to the end of the sequence. AsEnumerable<TSource>(IEnumerable<TSource>) Returns the input typed asIEnumerable<T>. Average<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>) ...
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 ...
下例示範如何建立名為AlternateElements的擴充方法,傳回集合中的每隔個項目,從第一個項目開始。 C# // Extension method for the IEnumerable<T> interface.// The method returns every other element of a sequence.publicstaticIEnumerable<T> AlternateElements<T>(thisIEnumerable<T> source) {int...