我有以下 linq 查询块来计算报告的一些值.
I have the following block of linq queries 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;
我已经在调试器中单步调试了代码,发现了一些奇怪的地方.赋值给test
后它的值为0.赋值给test2
后test2 == 0
和test == 11.31
test == 11.31
test2 == 11.28
和 test3 == 22.59
分配到 ExtraSales
后 test == 11.31
<代码>ExtraSales == 11.31.当这一切完成时,ExtraSales
中的值应该是 22.59.这是怎么回事?
I've stepped through the code in a debugger and I've noticed some oddities. After assigning into test
its value is 0. After assigning into test2
test2 == 0
and test == 11.31
after assigning into test3 test == 11.31
test2 == 11.28
and test3 == 22.59
after assigning into ExtraSales
ExtraSales == 11.31
. The value in ExtraSales
when this is all complete should be 22.59. What's going on here?
我在分配到 ExtraSales
后添加了额外的行,但值没有改变.
I've added additional lines after the assignment into ExtraSales
but the value does not change.
说这是一个延迟执行问题的答案是错误的. 这是一个运算符优先级问题.
The answers that say that this is a deferred execution problem are wrong. It is an operator precedence problem.
摆脱那里所有完全不相关且无法阅读的代码.这都是红鲱鱼.相关的重现是:
Get rid of all that completely irrelevant and impossible-to-read code in there. It is all red herring. The relevant repro is:
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;
最后一行是什么意思?和前面那行意思一样吗?
不,它没有.加法运算符比空合并运算符优先级,所以这是
No, it does not. The addition operator is higher precedence than the null coalescing operator, so this is
decimal test4 = d1 ?? (0m + d2) ?? 0m;
您编写的代码表示如果 d1 不为空,则生成 d1 的值.如果 d1 为空且 0m + d2 不为空,则生成 0m + d2 的值.如果 0m + d2 为空,则生成该值0米."
The code you wrote means "produce the value of d1 if d1 is not null. If d1 is null and 0m + d2 is not null then produce the value of 0m + d2. If 0m + d2 is null then produce the value 0m."
(您可能不知道 ?? 运算符具有这种令人愉快的链接属性.通常,a ?? b ?? c ?? d ?? e
为您提供第一个非空值a、b、c 或 d 的值,如果它们都为 null,则 e 的值.您可以根据需要制作链.这是一个非常优雅的小运算符.)
(You might not have known that the ?? operator has this pleasant chaining property. In general, a ?? b ?? c ?? d ?? e
gives you the first non-null value of a, b, c or d, and e if they are otherwise all null. You can make the chain as long as you like. It's quite an elegant little operator.)
由于 d1 不为空,所以我们生成它的值并且 test4 被分配了 d1 的值.
Since d1 is not null, we produce its value and test4 is assigned the value of d1.
你可能想说:
decimal test4 = (d1 ?? 0m) + (d2 ?? 0m);
如果您的意思是d1 或 d2 可能为空,如果其中之一为空,则将空值视为零".所以 12 + 2 是 14,12 + null 是 12,null + null 是 0
If you mean "d1 or d2 could be null, and if either is, then treat the null one as zero". So 12 + 2 is 14, 12 + null is 12, null + null is 0
如果您的意思是d1 和 d2 都可以为 null,如果 either 为 null,那么我想要零",那就是
If you mean "either d1 and d2 could be null, and if either is null then I want zero", that's
decimal test4 = (d1 + d2) ?? 0m;
所以 12 + 2 是 14,12 + null 是 0,null + null 是 0
So 12 + 2 is 14, 12 + null is 0, null + null is 0
我注意到,如果您已将代码格式化为相关文本显示在屏幕上,您可能不会首先发布五个左右的错误答案.尝试格式化您的代码,使其全部显示在屏幕上;如果你这样做,你会得到更好的答案.
I note that if you had formatted your code so that the relevant text was on the screen, you probably wouldn't have gotten five or so incorrect answers posted first. Try to format your code so that all of it is on the screen; you'll get better answers if you do.
这篇关于为什么我的 linq 查询中的值不立即出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!