LinqAF icon indicating copy to clipboard operation
LinqAF copied to clipboard

Select and Count

Open jackmott opened this issue 6 years ago • 1 comments

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.

jackmott avatar Feb 04 '19 14:02 jackmott

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;
}

manofstick avatar Feb 10 '19 08:02 manofstick