Comment

Comment by mrcsharp

As a side note: please avoid, as much as possible, putting `.Select(..)` before `.Where(..)`. You are wasting CPU cycles and memory space by forcing LINQ to map all the items and then filtering on the mapped value.

In most situations, you should be able to filter on the source enumerable before mapping making the whole thing more efficient.

Additionally, that `.Cast<TR>(..)` at the end should have been a dead giveaway that you are going down the wrong path here. You are incurring even more CPU and Memory costs as the `.Cast<TR>(..)` call will now iterate through all the items needlessly.[1]

Also, the design of this this method doesn't seem to make much difference to me anyways:

``` var strs = source.SelectNotNull(it => it); ```

vs

``` var strs = source.Where(it => it != null); ```

A lot of other LINQ extension methods allow you to pass in a predicate expression that will be executed on the source enumerable:

``` var str = source.First(it => it != null); ```

[1] https://source.dot.net/#System.Linq/System/Linq/Cast.cs,152b...

parent mrcsharp 1 year ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton