Why don't the values ​​from my linq queries immediately appear?

I have the following linq query block to calculate some values ​​for a report.

var items = (from trans in calclabordb.Sales_Transactions                
             select trans).SelectMany(st => st.Sales_TransactionLineItems).Where(stli => stli.TypeID == typeID);

decimal test = items.Where(stli => stli.Inventory_Item is Base).Sum(stli => (decimal?)stli.Inventory_Item.IntExtraServiceAmount) ?? 0;
decimal test2 = items.Where(stli => stli.Inventory_Item is Extra).Sum(stli => (decimal?)stli.ItemPrice) ?? 0;
decimal test3 = test + test2;
current.ExtraSales = items.Where(stli => stli.Inventory_Item is Base).Sum(stli => (decimal?)stli.Inventory_Item.IntExtraServiceAmount) ?? 0 +
    items.Where(stli => stli.Inventory_Item is Extra).Sum(stli => (decimal?)stli.ItemPrice) ?? 0;

I went through the code in the debugger, and I noticed some oddities. After assignment, testits value is 0. After assignment to test2 test2 == 0and test == 11.31after assignment to test3 test == 11.31 test2 == 11.28and test3 == 22.59after assignment to ExtraSales ExtraSales == 11.31. The value in ExtraSales, when all this is completed, should be 22.59. What's going on here?

EDIT: I added extra lines after assignment to ExtraSales, but the value does not change.

+5
source share
3 answers

, , , . .

. . :

decimal? d1 = 11.31m;
decimal? d2 = 11.28m;
decimal test1 = d1 ?? 0m;
decimal test2 = d2 ?? 0m;
decimal test3 = test1 + test2;
decimal test4 = d1 ?? 0m + d2 ?? 0m;

? , ?

, . , ,

decimal test4 = d1 ?? (0m + d2) ?? 0m;

, , " d1, d1 . d1 null 0m + d2 , 0m + d2. 0m + d2 null, 0".

(, , . a ?? b ?? c ?? d ?? e a, b, c d e, null. , . .)

d1 , , test4 d1.

, :

decimal test4 = (d1 ?? 0m) + (d2 ?? 0m);

, "d1 d2 , , ". , 12 + 2 14, 12 + null 12, null + null - 0

" d1 d2 , , ",

  decimal test4 = (d1 + d2) ?? 0m;

, 12 + 2 14, 12 + null 0, null + null 0

, , , , , , . , ; , .

+22

, , , , , ...

-1

All Articles