I was presented with a scenario recently which I felt an ideal opportunity to begin my adventures into LINQ.
Essentially I had to parse a collection setting a property in objects within the collection based on a condition.
The requirement is more obvious when presented in its standard iterative solution:
//standard iteration foreach (LogFileDataItem lfd in logFileData) { if (lfd.FileName.Equals(currentLine, StringComparison.OrdinalIgnoreCase)) { lfd.SQLScriptExecuted = true; } }
Quite simple really.
So how would we express this in LINQ? Well, the following ought to suffice:
IEnumerable retrievedScripts; retrievedScripts = from logFileDataItem in logFileData where logFileDataItem.FileName.Equals(currentLine) select logFileDataItem; foreach (LogFileDataItem lfd in retrievedScripts) { lfd.SQLScriptExecuted = true; }
However, the above is more verbose than its predecessor, so we can refactor, removing the IEnumerable retrievedScripts variable:
foreach (LogFileDataItem lfd in from logFileDataItem in logFileData where logFileDataItem.FileName.Equals(currentLine) select logFileDataItem) { lfd.SQLScriptExecuted = true; }
Ahhh, but wait, aren’t Lambda Expressions even more concise? Well, yes, so here it is in Lambda Expressions:
//can be expressed in lambda expressions as: foreach (LogFileDataItem lfd in logFileData.Where(item => item.FileName.Equals(currentLine) .Select(item => item)) { lfd.SQLScriptExecuted = true; }
But which is best? Time to consult Stackoverflow?
Well, for starters, the .Select(item => item) is totally useless. So, happy days! More refactoring:
foreach (LogFileDataItem lfd in logFileData.Where(item => item.FileName.Equals(currentLine))) { lfd.SQLScriptExecuted = true; }
And John Skeet (no less!) recommends we actually move the where clause out. Who am I to argue?
IEnumerable lines = logFileData.Where(item => item.FileName.Equals(currentLine)); foreach (LogFileDataItem lfd in lines) { lfd.SQLScriptExecuted = true; }
And as for their relative differences? Well, not much as it would appear. It’s really just a matter of personal preference.