IEnumerable<MyClass> objects = ...
foreach(MyClass obj in objects)
{
if(obj.someProperty != null)
SomeFunction(obj.someProperty);
}
I get the feeling I can write a smug LINQ version using a lambda but all my C# experience is ‘classical’ i.e more Java-like and all this Linq stuff confuses me.
What would it look like, and is it worth doing, or is this kind of Linq usage just seen as showing off “look I know Linq!”
You can move the if statement into a Where clause of Linq:
Going further, you can use List’s
ForEachmethod:That’s making the code slightly harder to read, though. Usually I stick with the typical
foreachstatement versus List’sForEach, but it’s entirely up to you.