software solutions / project leadership / agile coaching and training

A lesson in LINQ deferred execution

Posted on March 28, 2015

I learned something interesting about LINQ today, and I’m surprised that it took me this long to run into this.

Do you think this test would pass?

public class Account
{
    public int AccountId { get; set; }
    public string Status { get; set; }
}

[TestClass]
public class Test
{
    [TestMethod]
    public void Deferred_execution_test()
    {
        var accountIds = new[] {1, 2, 3};
        IEnumerable list = accountIds.Select(accountId => new Account {AccountId = accountId});
        
        foreach (var account in list)
            account.Status = "Active";

        foreach (var account in list)
            Assert.IsTrue(account.Status == "Active");
    }
}

It actually fails.

I'm sure when you've been debugging code you've at some point seem something like this:

The key is the "Expanding the Results View will enumerate the IEnumerable". This means that it hasn't yet executed the code in the Select() clause. So every time you access the IEnumerable<T>, it's going to execute the Select() statement, which means that it will new up new Account objects every time.

This test will pass:

public class Account
{
    public int AccountId { get; set; }
    public string Status { get; set; }
}

[TestClass]
public class Test
{
    [TestMethod]
    public void Deferred_execution_test()
    {
        var accountIds = new[] {1, 2, 3};
        IEnumerable list = accountIds.Select(accountId => new Account {AccountId = accountId}).ToList();
        
        foreach (var account in list)
            account.Status = "Active";

        foreach (var account in list)
            Assert.IsTrue(account.Status == "Active");
    }
}

All I did was add .ToList() to the end of the statement after the Select(). This executes the Select() statements and stores and actual List<T> in the "list" variable instead of a WhereSelectArrayIterator (which has deferred execution).

Just something to keep in mind if you're using Select() to new up some objects.

1 Comment »

  1. yes ienumerables are read only.

    Ben — March 11, 2017 @ 9:50 pm

Leave a comment





SERVICES
SOFTWARE SOLUTIONS
I have over 15 years of software development experience on several different platforms (.NET, Ruby, JavaScript, SQL Server, and more). I recognize that software is expensive, so I'm always trying to find ways to speed up the software development process, but at the same time remembering that high quality is essential to building software that stands the test of time.
PROJECT LEADERSHIP
I have experience leading and architecting large Agile software projects and coordinating all aspects of a project's lifecycle. Whether you're looking for technical expertise or someone to lead all aspects of an Agile project, I have proven experience from multiple projects in different environments that can help make your project a success.
PROCESS COACHING
Every team and every situation is different, and I believe that processes and tools should be applied with common sense. I've spent the last 10+ years working on projects using Agile and Lean concepts in many different environments, both in leadership roles and as a practitioner doing the work. I can help you develop a process that works best in your organization, not just apply a prescriptive process.
Have any questions? Contact me for more information.
PRESENTATIONS
Iteration Management - Your Key to Predictable Delivery
From Stir Trek 2016 and QA or the Highway 2015
From CodeMash 2016, QA or the Highway 2014, Stir Trek 2012
The Business of You: 10 Steps For Running Your Career Like a Business
From CodeMash 2015, Stir Trek 2014, CONDG 2012
From Stir Trek 2013, DogFoodCon 2013
(presented with Brandon Childers, Chris Hoover, Laurel Odronic, and Lan Bloch from IGS Energy) from Path to Agility 2012
From CodeMash 2012 and 2013
(presented with Paul Bahler and Kevin Chivington from IGS Energy)
From CodeMash 2011
An idea of how to make JavaScript testable, presented at Stir Trek 2011. The world of JavaScript frameworks has changed greatly since then, but I still agree with the concepts.
A description of how test-driven development works along with some hands-on examples.
From CodeMash 2010
From CodeMash 2010