Sami inherited some C# LINQ code. The actual behavior and purpose of this code is fairly simple. The way the code was written, however, well…

foreach (var operatingMode in ahu.CalculationData.OperatingModes) { operatingModesModel.OperatingModeNames.Add
(operatingModeNumber, operatingMode.OperatingModeName); var 
innerOperatingModeNumber = operatingModeNumber; foreach (var 
property in from partData in operatingMode.PartDatas.Where(p 
=> p.PartGuid == partGuid) let finalOperatingModeNumber = 
innerOperatingModeNumber from property in (from resultProperty 
in this.GetProperties(partData).Where(p => 
FilterAcceptNonSoundAndNonImageProperties(p, updateResult.For
(partData)) && (propertyFilterFunction?.Invoke(partData, p) ?? 
true)).ToList() let measurementUnit = resultProperty.Type.
GetPresentationMeasurementUnit(measurementUnits) let 
measurementUnitTranslationId = measurementUnit?.TextId select 
new OperatingModesModel.OperatingModePropertyModel
(finalOperatingModeNumber, this.TranslationService.
Type.NameId), this.PrintoutUtil.GetValueString(resultProperty, 
measurementUnit, this.Language), string.IsNullOrEmpty
(measurementUnitTranslationId) ? "-" : this.TranslationService.
(measurementUnitTranslationId), resultProperty.Key)) select 
property) { operatingModesModel.OperatingModeProperties.Add
(property); } operatingModeNumber++; }

Whitespace added for wrapping, as this is all one line in the submission.

Sami has this to say:

It blows my mind that someone would go this way to create such an unreadable nested mess with LINQ and leave it as is for the next developers to handle. It's no wonder the original writers of this code no longer desire to work on the project.

Now, we could try and unpick this code- it's a pretty straightforward series of nested loops and internal queries, but… why? I think we should learn a lesson from the original developers, and not put any more work into this than we have to.

Sami adds that this code is now obsolete, and replaced with something far simpler and easier to read.

[Advertisement] ProGet’s got you covered with security and access controls on your NuGet feeds. Learn more.