vkhorikov/CSharpFunctionalExtensions

Double enumeration in TryFirst

TechnoBerry opened this issue · 1 comments

Current implementation of TryFirst extension method leads unnecessary double enumeration (source.Any() and source.First()). It has no problem when source is a collection but when I use it in chain with LINQ it leads double applying of LINQ methods.

E.g.

var myCollection = new List<int> { 4, 8, 15, 23, 42 };

myCollection
  .Where(i => i % 2 == 0) // Called twice
  .Select(i => i * 10) // Called twice
  .TryFirst();

Sorry for the late reply. Yeah, this is an issue. I'm marking this as a bug, will fix this later. Thanks for reporting.