LinqAF
LinqAF copied to clipboard
Select and Count
I was reading an article about .NET Core, where early versions had a bug/feature where if you had something like collection.Select(foo).Count(); it wouldn't actually perform the Select operation, since it doesn't matter, you only need the count. But, since this would prevent side effects from happening, it was a breaking change and they had to fix it.
But, this seems like an optimization that LinqAF could keep. Maybe you already do, but if not just wanted to get the idea out there.
The did leave remnants of this optimization in there (which, you could argue, are breaking changes...):
var count = 0;
var ignore =
Enumerable
.Range(0, 100)
.Select(x => { ++count; return x; })
.Skip(90)
.ToList();
switch (count)
{
case 10: System.Console.WriteLine(".net core (probably...)"); break;
case 100: System.Console.WriteLine(".net framework (probably...)"); break;
default: System.Console.WriteLine("who knows?"); break;
}