LINQ, which stands for Language Integrated Query, isn’t new anymore. It was introduced in .NET 3.5, which saw its release in November 2007. At this point, I feel it should be a core skill for any seasoned .NET developer.
But every now and then, I see LINQ that makes me cringe. Before you write a line of LINQ, you should understand that it has deferred execution. That means it’s not executed as soon as it is called. Instead, the query waits as long as possible before returning results.
My personal philosophy is to take advantage of that delayed execution. The following line, when called needlessly, causes that cringing feeling I spoke of earlier:
var results = (from o inobjects
where o.something == "something"
The ToList() forces LINQ to loop through all the items in that enumerable. That’s all fine if it will always be executed, but what if the next line...