为什么不从我的LINQ查询的值立即出现? [英] Why don't the values from my linq queries appear immediately?

查看:112
本文介绍了为什么不从我的LINQ查询的值立即出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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;



我已经通过在调试器中的代码踩,我已经注意到了一些怪事。分配到测试后其值为0 test2的== 0分配到测试2 测试== 11.31 分配到TEST3后测试== 11.31 测试2 == 11.28 TEST3 == 22.59 分配到后 ExtraSales 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不为null。如果d1是空和0米+ D2不为空则产生0米+ d2的值,如果0米+ D2为null,则产生的价值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 13 C 22 D 41è给你的第一个非空值,b,C或D和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);



,或者甚至更好

or, even better

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



我注意,如果你已经格式化您的代码,使相应的文本是在屏幕上,你可能会不会得到先贴五年左右的不正确的答案。尝试格式化你的代码,使这一切是在屏幕上;如果你这样做,你会得到更好的答案。

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查询的值立即出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆