久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么我的 linq 查詢中的值不立即出現?

Why don#39;t the values from my linq queries appear immediately?(為什么我的 linq 查詢中的值不立即出現?)
本文介紹了為什么我的 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;

我已經在調試器中單步調試了代??碼,發現了一些奇怪的地方.賦值給test后它的值為0.賦值給test2test2 == 0test == 11.31test == 11.31 test2 == 11.28test3 == 22.59 分配到 ExtraSalestest == 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Why shouldn#39;t I always use nullable types in C#(為什么我不應該總是在 C# 中使用可空類型)
C# HasValue vs !=null(C# HasValue vs !=null)
C# ADO.NET: nulls and DbNull -- is there more efficient syntax?(C# ADO.NET:空值和 DbNull —— 有沒有更高效的語法?)
How to set null value to int in c#?(如何在c#中將空值設置為int?)
How to handle nulls in LINQ when using Min or Max?(使用 Min 或 Max 時如何處理 LINQ 中的空值?)
Method call if not null in C#(在 C# 中如果不為 null 的方法調用)
主站蜘蛛池模板: 亚洲日本免费 | 亚洲视频一区在线观看 | 久久9999久久 | 黄色一级大片在线观看 | 好姑娘影视在线观看高清 | 亚洲乱码一区二区 | 色婷婷国产精品综合在线观看 | 欧美精品a∨在线观看不卡 国产精品久久国产精品 | 欧洲精品久久久久毛片完整版 | 欧美日韩精品中文字幕 | 国产91在线观看 | 国产成人精品免高潮在线观看 | 国产成人久久精品 | 欧美大片一区 | 欧美日本高清 | 欧洲亚洲一区 | 国产福利在线 | 久久精彩 | 成人二区 | 亚洲色综合 | 国产伦精品一区二区三区高清 | 亚洲视频二区 | 一级日批片 | 中文字幕在线视频免费观看 | 国产一区二区麻豆 | av在线伊人 | 欧美日高清视频 | 久久久久久久国产精品影院 | 国产成人免费在线观看 | 亚洲精品日韩视频 | 国产人免费人成免费视频 | 亚洲精品一区二区三区蜜桃久 | 欧美黄色精品 | 亚洲日日夜夜 | 国产剧情一区 | 日本一区二区三区四区 | 一区二区免费在线视频 | 久久精品久久久久久 | 亚洲v日韩v综合v精品v | 天天精品在线 | 欧美日韩成人在线 |