I'm looking for something like the ForEach method but it only runs on one element.
// Do something with every element.
SomeList.ForEach(o => DoSomethingWith(o))
// Do something with just the first element, if any, or nothing at all for an empty list.
SomeLine.ForFirst(o => DoSomethingWith(o))
I'm trying to stick with a functional paradigm, and using the First
, FirstOrOptional
, FirstOrDefault
, seem to end up involving a lot of Null checking or exception handling.
What is the Linq one-line way of doing this?