This IS a delayed execution problem. However, my issue stems from the fact that I cannot ascertain WHY this execution is delayed sometimes, and not other times.
Code:
IList<LineItem> freeFlatItems = new List<LineItem>();
if(QualifyFreeFlatShipping)
freeFlatItems = lineItems.Where(l => l.FlatShippingRate != null).ToList();
decimal? freeFlatShippingTotal = freeFlatItems.Sum(l => l.FlatShippingRate);
var globalShippingPromos = _promoService.GetGlobalShippingPromos();
Problem:
This code is in production, and works as expected. I recently make changese elsewhere and found that this test was not working in our unit testing. When I step through this function the following happens:
- Before reaching this code, I verified the input data. All items in
lineItems. Each Item has as value for.FlatShippingRate - QualifyFreeFlatShipping is
true - Code Execution reaches the linq statment on line 4 (
freeFlatItems = etc...) - Value of
freeFlatItemsremains unchanged as execution continues to line 6 (decimal? freeFlatShippingTotal = etc...) .Sumis executed across an empty list.- Upon reaching line 8 (
var globalShippingPromos = etc...) the value offreeFlatItemsfinally updates to what it should be. However... sum was ran on the previous value, and my shipping total is incorrect.
The Question(s):
- Why is this still being delayed? I thought
.ToList()forced the execution of the linq to generate theIList<T> - Why does this behave differently in a consistant manner? (My Test always behaves this way, production works fine, this code in LinqPad works fine). And before you suggest it, I have validated my test is built up correctly. Both in the test code and in step one above by validating the input data in the debugger.