Last() throws an exception when applied on a concatenation of a collection with two empty collections #108477
Labels
area-System.Linq
bug
in-pr
There is an active PR which will close this issue when it is merged
regression-from-last-release
Milestone
Description
Hi, we've run into an issue with .NET 9, preview 2 onwards. If you have an enumerable with some elements and you concatenate it with other two enumerables which are empty the Last() method, applied to the result concatenation, throws an InvalidOperationException 'Sequence contains no elements'.
Reproduction Steps
Same code in a project: https://github.com/AnnaVel/ConcatenatedLast
Expected behavior
Last() should return 3.
Actual behavior
Last() throws an exception.
Regression?
Yes. This does not reproduce in .NET 9 preview 1, but reproduces in .NET 9 preview 2 and later.
Known Workarounds
One could just use collection.ElementAt(collection.Count() - 1).
Configuration
It is not likely this is a configuration-specific problem.
Other information
No response
The text was updated successfully, but these errors were encountered: