I am looking for an explanation of why the following line of code is allowed to compile:
var results = someCollection.Where(x => x.SomeBooleanProperty = true);
Pay attention to using a single equality operator (perhaps the developer was in SQL mode), which is very easy to do. This compiles, and when the results are evaluated (for example, someCollection.ToList()), it changes the flag to true in the entire collection!
If you use an entity infrastructure or any other ORM, then this can be detected as a change. I just ran into this problem in production code, but luckily it only caused a minor (but completely bamboo) problem on the read-only screen. Imagine the terrible logic and data problems that could be caused by this if the data was actually saved.
Just to make sure that I'm not losing my mind and that it really changes the data that I wrote, a test that fails:
[Test]
public void Test_because_im_scared()
{
var falseProperty = new TestModel {BooleanProperty = false};
var trueProperty = new TestModel {BooleanProperty = true};
var list = new List<TestModel>{falseProperty, trueProperty};
var results = list.Where(x => x.BooleanProperty = true);
Assert.IsFalse(falseProperty.BooleanProperty);
Assert.IsTrue(trueProperty.BooleanProperty);
var evaluatedResults = results.ToList();
Assert.IsFalse(falseProperty.BooleanProperty);
Assert.IsTrue(trueProperty.BooleanProperty);
}
source
share