Now that we have tremendous functionality thanks to LINQ, I’m wondering which syntax is preferable. For example, I found the following method (just thought it was a good example):
foreach (FixtureImageServicesData image in _fixture.Images) { if (image.Filename != _selectedFixtureImage.Filename && image.IsPrimary) { image.IsPrimary = false; image.IsChanged = true; } }
If we were to convert it to a LINQ approach, it would look like this (not tested):
_fixture.Images.Where(x => x.Filename != _selectedFixtureImage.Filename && x.IsPrimary).ForEach(x => { x.IsPrimary = false; x.IsChanged = true; });
Which would you would rather see and maintain? Is this crazy or genius?
Using a
ForEachextension method is okay, but there’s an intermediate approach:If you do use a
ForEachmethod, I’d definitely format it in multiple lines:(Reduced indentation to avoid wrapping…)
or:
You might even want to extract the ‘making non-primary’ bit into a separate method, at which point you’d have: